问题
I want to pass value of textfield in LoginView to uploadview but UserLogin1 always is NULL ( 0x000000) This is my codes: loginvuew.h
@property (nonatomic,retain) IBOutlet UITextField *username;
loginview.m
-(IBAction)LoginButton:(id)sender{
uploadview= [[UploadTab alloc] initWithFrame:CGRectMake(0,0,0,0) andUsertring:username.text];
}
uploadview.h
@property (nonatomic, copy) NSString *UserLogin1;
- (id)initWithFrame:(CGRect)frame andUsertring:(NSString *)strUser;
uploadview.m
- (id)initWithFrame:(CGRect)frame andUsertring:(NSString *)strUser
{
if (self) {
UserLogin1=[[NSString alloc] init];
UserLogin1 = strUser;
}
return self;
}
- (void)viewDidLoad
{
NSLog(@"User login: %@",UserLogin1);
self.navigationItem.title = [NSString stringWithFormat: @"Hello, %@",UserLogin1];
}
WHen run in initWithFrame(), string passing correct but in ViewDidLoad is not correct. And UserLogin1 variable always is 0x00000. DO you have sugesstions? Thansk in advance
回答1:
You are messing up things I think. What class is an UploadTab ? The view does not have a method viewDidload ! It's UIViewController's one. If it is ViewController then you should allocate it properly with initWithNibName:bundle or smth like that. ADDED: Your code missing actual alloc-init method
- (id)initWithFrame:(CGRect)frame andUsertring:(NSString *)strUser
{
// self is nil here! It is in almost every cases done like self = [[super alloc] initBlaBla]
if (self) {
UserLogin1=[[NSString alloc] init];
UserLogin1 = strUser;
}
return self;
}
来源:https://stackoverflow.com/questions/17647898/can-not-pass-value-of-textfield-to-other-viewcontroller-in-ios