【问题标题】:Can not pass value of textfield to other viewcontroller in ios无法将文本字段的值传递给 ios 中的其他视图控制器
【发布时间】:2013-07-15 06:22:25
【问题描述】:

我想将 LoginView 中文本字段的值传递给 uploadview,但 UserLogin1 始终为 NULL(0x000000) 这是我的代码: 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];
}

上传视图.h

@property (nonatomic, copy) NSString *UserLogin1;
- (id)initWithFrame:(CGRect)frame andUsertring:(NSString *)strUser;

上传视图.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];

}

当在 initWithFrame() 中运行时,字符串传递正确但在 ViewDidLoad 中不正确。 UserLogin1 变量始终为 0x00000。你有什么建议吗?提前致谢

【问题讨论】:

  • 您是否已经尝试设置UserLogin1 = [strUser copy];
  • @geo: 我试过了,但是不行
  • 顺便说一句,UserLogin1 是一个变量而不是一个类,它应该以小写字母开头(以符合编程约定)。
  • @geo:谢谢,但我认为它没用
  • 使用委托或NSUserDefaults传递数据:stackoverflow.com/questions/17396456/…

标签: iphone ios objective-c uitextfield delegation


【解决方案1】:

你把我认为的事情搞砸了。 UploadTab 是什么类?该视图没有方法 viewDidload !这是 UIViewController 的一个。如果是 ViewController,那么您应该使用 initWithNibName:bundle 或类似的方式正确分配它。 添加: 您的代码缺少实际的 alloc-init 方法

- (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;
}

【讨论】:

  • UploadTab 是 ViewController 类型。
  • 我实现了 - (id)initWithFrame:(CGRect)frame andUsertring:(NSString *)strUser;但它不起作用
  • 肯定不会,因为 UIViewController 没有这样的方法..你应该在你的方法中调用标准 init 之一
  • 谢谢,我试过了 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andUsertring:(NSString *)strUser { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // 自定义初始化 userLogin1 = strUser; } 返回自我;但它不起作用,userLogin1 在 Viewdidload 中始终为 NULL
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-24
  • 2011-05-12
  • 1970-01-01
  • 2016-11-13
相关资源
最近更新 更多