ios - 通过导航栏的后退按钮传递 bool 值

标签 ios delegates protocols parameter-passing

我有两个viewController,viewControllerB,当通过navigationBar的后退按钮返回viewControllerA时,我希望你传递一个 bool 值。我使用了一个协议(protocol),但不起作用。

在ViewControllerB.h中

 @protocol detailProgrammFiereDelegate <NSObject>
 @required
 -(void) addItemViewController: (ViewControllerB *)programmFiere withBool:(BOOL)booleanFiere;
 @end

 .....
 @property (nonatomic, weak)id <detailProgrammFiereDelegate>delegate;
 .......

在ViewControllerB.m中

- (void)viewDidLoad
{
     ......

     BOOL booleanFiere =YES;
    [self.delegate addItemViewController:self withBool:booleanFiere];
 }

在ViewControllerA.h中

@interface ViewControllerA: UIViewController <detailProgrammFiereDelegate>

在ViewControllerA.m中

-(void)addItemViewController:(DetailProgrammFiere *)programmFiere withBool:(BOOL)booleanFiere{

     //after pressing back button of viewControllerB not enter into this method. Why?

    if (booleanFiere){ //is already true before opening the ViewControllerB. Why?
        [self viewDidLoad];
    }
}
........

 -(void)getInformationsFiere:(id)sender{ //method that open ViewControllerB

     ViewControllerB * detailFiere =[[ViewControllerB alloc]initWithNibName:@"ViewControllerB~iPhone" bundle:nil];


    detailFiere.delegate =self;


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

   }

在打开 ViewControllerB 之前 bool 值已经为 true,这不应该发生。

最佳答案

如果要从B返回A时传递参数,则不应将调用委托(delegate)方法放在viewDidLoad中。 B 的 viewDidLoad 会在 B 被 alloc 和 init 的时候调用,但在 pop B 返回到 A 的时候不会被调用。这也是为什么在 B 出现之前,A 的 booleanFiere 就已经是 YES 的原因了。

你可以放

    [self.delegate addItemViewController:self withBool:booleanFiere];

就在 B 的 [self.navigationController popViewControllerAnimated:YES] 之前,而不是在 B 的 viewDidLoad 中。所以A的-addItemViewController:会在返回时被调用

关于ios - 通过导航栏的后退按钮传递 bool 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13836809/

相关文章:

ios - 在 iOS Xamarin Native 中设置 FSCalendarAppearance 委托(delegate)

ios - 如何将自定义委托(delegate)设置为 UITableViewController?

ios - 如何从 viewController 到 appdelegate 进行通信?

ios - ios的折叠工具栏布局

来自 DLL 的 C# 回调

ios - Swift 类的 Objective-c 协议(protocol)。无法识别的方法

xcode - 使用枚举的默认实现扩展协议(protocol)会导致 Xcode 崩溃

swift - 多个协议(protocol)扩展中的模糊函数?

ios - 容器 View Controller 如何在 sizeForChildContentContainer 中查找和提供 subview Controller 的大小

ios - 关闭另一个 View 后,UITableView 滚动内容大小不正确