在ViewController之间传递布尔值

时间:2012-10-12 08:11:58

标签: ios uiviewcontroller messaging

我必须将一个布尔值从ViewControllerA传递给ViewControllerB。在ViewControllerA中,我将其作为属性:

 @property (nonatomic, assign)BOOL fromViewControllerA;

然后打开ViewControllerB:

   ViewControllerB * viewControllerB = [[ViewControllerB alloc]initWithNibName:@"ViewControllerB" bundle:nil];

    self.fromRootViewController = YES;

    [self.navigationController pushViewController:viewControllerB animated:YES];

在ViewControllerB中,我导入了ViewControllerA并创建了属性:

   @property (nonatomic, strong) ViewControllerA *viewControllerA

稍后在ViewDidLoad()

    if (viewController.fromViewController){
       //.......
    }

不起作用。为什么呢?

3 个答案:

答案 0 :(得分:1)

在viewController中创建BOOL属性B说.. BOOL setFlag;在ViewController B中合成它,

现在在ViewController A中执行push

 ViewControllerB * viewControllerB = [[ViewControllerB alloc]initWithNibName:@"ViewControllerB" bundle:nil];

    self.fromRootViewController = YES;
    viewControllerB.setFlag = YES;  //set the value here 

    [self.navigationController pushViewController:viewControllerB animated:YES];

答案 1 :(得分:0)

是不是因为您应该将第二行修改为 viewControllerB .fromRootViewController? (而不是自我)

答案 2 :(得分:0)

如果您想知道视图控制器的来源,则不应使用属性。在导航控制器阵列中使用基本导航(在您的班级中想要了解推送的来源)。

NSArray *controllers = [self.navigationController viewControllers];
UIViewController *lastController = [controllers objectAtIndex:[controllers count]-2];
if([controller isKindOfClass:[YourDesireCheckController class]])
    return YES;
return NO;