Can not pass value of textfield to other viewcontroller in ios

蓝咒 提交于 2020-01-06 20:37:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!