簡體   English   中英

當我的根視圖轉到另一個視圖時,該視圖是否可以更改根視圖?

[英]When My root view goes to another View, is there a way for that view to make changes to the root View?

我的根視圖有一個名為update的按鈕(已隱藏)和一個名為cHome的類。
當用戶退出RootView並轉到另一個視圖時,我稱為cEdit並具有一個稱為cEdit的類。 這個視圖有沒有辦法在我的rootview上設置更新按鈕的隱藏狀態?所以當它調用[self.navigationController popViewControllerAnimated:YES]; 返回到根視圖,更新按鈕將具有新的隱藏狀態?

返回到根視圖,更新按鈕將具有新狀態?

在您的根視圖控制器上,您可以執行以下操作:這樣,當您稍后調用后返回根視圖控制器時: [self.navigationController popViewControllerAnimated:YES]; 該按鈕將不再隱藏。

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];

    self.update.hidden = NO;
}

有一種方法可以執行您想要的操作,您可以實現自己的委托。

例如 :

您在.h中創建一個新的類文件,然后添加此代碼

#import <UIKit/UIKit.h>

@protocol ClassNameDelegate <NSObject>

@optional

- (void) tellUpdateButtonToHide;

@end


@interface ClassNameViewController : UIViewController
@property (weak,nonatomic)  id <ClassNameDelegate>delegate;

您創建自己的委托,並且如果您想訪問協議,則必須添加一個屬性

在您的.m文件中,我只是在另一個控制器(不是rootVc)中添加一個按鈕,該按鈕將更新rootController

- (IBAction)tellUpdateButtonToHide:(id)sender {

   [self.delegate tellUpdateButtonToHide];

}

在您的rootVC .h文件中,您導入了委托類

#import "ClassNameViewController.h"

@interface RootVcViewController : UIViewController<ClassNameViewControllerDelegate>

在您的rootVc .m文件中,實現方法tellUpdateButtonToHide;

- (void)tellUpdateButtonToHide{
   [self dismissViewControllerAnimated:YES completion:nil];
   [self.updateButton setHidden:NO];
 }

並在您的prepareForSegue中

if ([segue.identifier isEqualToString:@"da"] ) {
  ClassNameViewController *vc =(ClassNameViewController   *)segue.destinationViewController;
    [vc setDelegate:self];
}

希望它對你有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM