[英]Changing ViewController from an UIImage
我試圖在按下圖片中的某個區域時更改ViewController。 我嘗試這樣做:
-(void)openDrawer{
UIStoryboard *mStoryBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc =[mStoryBoard instantiateViewControllerWithIdentifier:@"DrawViewController"];
[self.navigationController pushViewController:vc animated:YES];
}
但是self.navigationController不存在,可以理解,但是我找不到在Image中獲取當前viewController的方法
我嘗試將其放在不同的類中,從當前視圖的viewController委托。
-(void) changeChangeViewToName:(NSString *)name{
UIStoryboard *mStoryBoard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *vc =[mStoryBoard instantiateViewControllerWithIdentifier:name];
[self.navigationController pushViewController:vc animated:YES];
}
這是從viewController內部工作的,但是我不能從Image內部調用函數。
從類(圖像?)發布一個通知,以決定要顯示新的視圖控制器。 在能夠執行推送的現有視圖控制器中偵聽該通知。
菲利普·米爾斯(Phillip Mills)是對的。 您還可以使用委托模式:
為應該能夠執行所需操作的對象創建協議:
@protocol YourImageDelegate \n -(void)touchUpInsideArea;\n @結束
將此方法添加到您的視圖控制器:
-(void)touchUpInsideArea {\n [self openDrawer];\n }
設置您的視圖控制器以符合協議:
@interface YourViewController:UIViewController
並且不要忘記在兩個文件中都包含您的協議:視圖控制器類和圖像類。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.