[英]iOS - Adding a new view / Switching views on button press
我有一個拆分視圖應用程序,在詳細信息視圖中有一個按鈕,單擊該按鈕將使用戶進入所選圖像的全屏視圖。
我知道我需要一個新的nib文件和視圖控制器,但我不知道如何將這些新文件與我現有的RootViewController和DetailViewController文件連接起來。
我知道這真的很模糊,但任何幫助都會非常感激。
只要您移動到單個視圖(如在另一個拆分視圖中),您應該只需要一個視圖控制器。 如果我理解你在做什么,那么進展應該是這樣的:
在DetailViewController.h中聲明新視圖控制器的特定實例,在本例中稱為newViewController,並在DetailViewController.m中合成它。
@interface DetailedViewController { NewViewController *newViewController; } @property (nonatomic, retain) NewViewController *newViewController; @end
將您的IBAction添加到DetailViewController的頭文件中,這將是負責實際觸發視圖切換的函數
在DetailViewController.m文件中實現視圖切換操作,應該如下所示:
(IBAction)switchToNewView:(id)sender { if (newViewController == nil) { NewViewController *newViewController = [[NewViewController alloc] initWithNibName:@"NewViewController" bundle:[NSBundle mainBundle]]; self.newViewController = newViewController; } // How you reference your navigation controller will // probably be a little different [self.navigationController pushViewController:self.newViewController animated:YES]; }
然后在viewDidLoad函數里面的DetailViewController.m文件中添加以下內容:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemAdd target:self action:@selector(switchToNewView:)]; self.navigationItem.rightBarButtonItem = addButton; [addButton release];
如果您選擇通過UIButton實現此選項,則另外一個選項是在NewViewController.xib上的Interface Builder中創建按鈕,然后選擇它,並在Connections檢查器中,在“touchUpInside”事件之間創建一個鏈接和文件所有者,然后選擇您的switchToNewView IBAction。 這應該完成同樣的事情。
這是一般的想法。 我希望有所幫助!
編輯:如評論中所述,如果將一個按鈕添加為UIBarButton作為導航控制器的一部分,您只需要執行以下操作:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(switchToNewView:)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.