0

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

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

    [self.tabBarController setSelectedIndex:1];

 }

CreateMatchTableViewController是我的第二个视图控制器和第二个视图控制器中的createBtnPressediaNSString对象。

viewDidLoad第二个视图控制器中:

- (void)viewDidLoad
{
    [super viewDidLoad];

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

    NSString *stringFromFirstView = createBtnPressed;

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

请帮忙!!

4

3 回答 3

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];

}
于 2013-05-02T08:36:27.480 回答
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];

}
于 2013-05-02T09:52:51.490 回答
-1

尝试

    NSString* a = [[NSString alloc] initWithString:createBtnPressed];
于 2013-05-02T08:37:39.273 回答