传递的NSString始终为null

时间:2013-05-02 08:25:37

标签: ios objective-c uiviewcontroller nsstring

我需要将NSString从一个视图传递到另一个视图。我在我的first view controller

-(IBAction)btnCreate_click:(id)sender
{
    CreateMatchTableViewController *matcObj = [[CreateMatchTableViewController alloc]init];
    matcObj.createBtnPressed = @"pressed";

    [self.tabBarController setSelectedIndex:1];

 }

CreateMatchTableViewController是我在第二个视图控制器中的第二个视图控制器和createBtnPressed ia NSString对象。

在第二个视图控制器的viewDidLoad中:

- (void)viewDidLoad
{
    [super viewDidLoad];

   // MatchListViewController *obj = [[MatchListViewController alloc]init];

    NSString *stringFromFirstView = createBtnPressed;

    NSLog(@"check===>%@",stringFromFirstView);//...always null
}

请帮助!!

3 个答案:

答案 0 :(得分:1)

似乎在您的第一个视图控制器中,您正在创建CreateMatchTableViewController的新实例。你不希望这样,因为你已经在标签栏控制器中有你的CreateMatchTableViewController实例......

尝试这样的事情:

-(IBAction)btnCreate_click:(id)sender
{
    CreateMatchTableViewController *matcObj = [self.tabBarController.viewControllers objectAtIndex:1];
    matcObj.createBtnPressed = @"pressed";

    [self.tabBarController setSelectedIndex:1];

}

修改

-(IBAction)btnCreate_click:(id)sender
{
    UINavigationController *navigationController = [self.tabBarController.viewControllers objectAtIndex:1];

    CreateMatchTableViewController *matcObj = [[navigationController viewControllers] objectAtIndex:0];// I put 0 assuming you have a CreateMatchTableViewController as the navigation controller's root view controller
    matcObj.createBtnPressed = @"pressed";

    [self.tabBarController setSelectedIndex:1];

}

答案 1 :(得分:0)

在CreateMatchTableViewController上编写如下方法:

(void)setStringFromFirstViewController:(NSString*)str{
    NSString *stringFromFirstView = str;
    NSLog(@"check===>%@",stringFromFirstView);
}

现在在“第一个视图控制器”上执行以下操作:

-(IBAction)btnCreate_click:(id)sender{
    CreateMatchTableViewController *matcObj = [[CreateMatchTableViewController alloc]init];
    [matcObj setStringFromFirstViewController: @"pressed"];
    [self.tabBarController setSelectedIndex:1];

}

答案 2 :(得分:-1)

尝试

    NSString* a = [[NSString alloc] initWithString:createBtnPressed];