[英]How can I push a new viewController into an existing view within another view controller?
[英]How can I push a new View Controller from a button within a subView (another View Controller) in Storyboard
哇,我對這個問題想了個很大的想法!
因此,我在另一個視圖控制器(主VC)中有一個名為“ ContentView”的視圖控制器。 主VC具有使用情節提要創建的導航控制器。 而且contentView加載3個不同的視圖控制器(vc1,vc2和vc3),具體取決於分段控件具有的選項。 所以現在的問題是:
如何從用戶從分段控件中選擇選項后出現的子視圖(vc2)之一中的按鈕加載新的View Controller?
盡管我的情節提要板上有可見的視圖控制器(vc2),但由於導航控制器位於主VC上,因此顯然無法將按鈕的操作連接到vc2文件的所有者。
我嘗試使用以下代碼訪問它:
AppDelegate *del = (AppDelegate *)[UIApplication sharedApplication].delegate; UINavigationController *navigationController = (UINavigationController*)del.window.rootViewController; DetalleMisMarcas *detalleMarcas = [[DetalleMisMarcas alloc] initWithNibName:@"DetalleMarcas" bundle:nil]; [navigationController pushViewController:detalleMarcas animated:YES];
但這行不通。
我試圖從該論壇中找到解決方案,但是我沒有運氣。 大多數人認為Xcode 4.2不存在主窗口。
最后,我在這里加載3個子視圖的方式是:
-(IBAction)segmentCtrlChanged:(id)sender {
UISegmentedControl *seg = sender;
if (seg.selectedSegmentIndex == 0)
{
MMViewController *mm= [self.storyboard instantiateViewControllerWithIdentifier:@"MMView"];
mm.view.frame = CGRectMake(0, 0, 320, 240);
[contentView addSubview:mm.view];
}
else if (seg.selectedSegmentIndex == 1) {
MPViewController *mp = [self.storyboard instantiateViewControllerWithIdentifier:@"MPView"];
mp.view.frame = CGRectMake(0, 0, 320, 240);
[contentView addSubview:mp.view];
}
}
-(IBAction)mainSubView:(id)sender
{
MMViewController *mm = [[MMViewController alloc] initWithNibName:@"MTView" bundle:nil];
[contentView addSubview:theMTView];
mm.view.frame = CGRectMake(0, 0, 320, 240);
}
有任何想法嗎?
編輯:根據您的評論,我認為代表們會更好地工作。 這就是您可以實現它的方式。 假設在contentView控制器類中添加了一個委托
// in the header file @property (weak, nonatomic) id <ContentViewControllerDelegate> delegate; @protocol ContentViewControllerDelegate <NSObject> - (void)contentViewDidSelectAView:(UIView *)view; @end // in the implementation - (IBAction)segmentCtrlChanged:(UISegmentedControl *)sender { case 0: [self.delegate contentViewDidSelectAView:[[self.storyboard instantiateViewControllerWithIdentifier:@"MMView"] view]; break; // and the rest of the code } // in MainView header file @interface MainViewController : UIViewController <ContentViewControllerDelegate> // in MainView implementation - (void)contentViewDidSelectAView:(UIView *)view { [self.view addSubView:view]; }
我實際上尚未實現上述代碼。 但是我認為這應該是控制器與父代對話的方式。 希望這對您有所幫助。
我不確定您到底在做什么,但是假設您的根目錄中有一個UINavigationController,並且mainViewController通過segue作為關系進行鏈接。 實施后,應通過valueChange
事件將mainVC中的segmentedControl鏈接到-(IBAction)segmentCtrlChanged:(id)sender
。 在這種方法中,盡管它們的工作原理相同,但是您可以切換索引,而不是使用if,並且還可以使用UISegmentedControl *
代替id
:
-(IBAction)segmentCtrlChanged:(UISegmentedControl *)sender { switch(sender.selectedSegmentIndex) { case 0: { MMViewController *mm= [self.storyboard instantiateViewControllerWithIdentifier:@"MMView"]; [self.navigationController pushViewController:mm animated:YES]; break; } case 1: { MPViewController *mp = [self.storyboard instantiateViewControllerWithIdentifier:@"MPView"]; [self.navigationController pushViewController:mp animated:YES]; break; } case 2: { // similar to above break; } default: break; }
我假設vc1,vc2和vc3是UIViewController的實例。 因此,請確保它們的類已鏈接,並且其標識符已正確設置。 然后,將它們保留原樣,因為您不會為它們單獨進行任何篩選。 它們將通過代碼被壓入堆棧。
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.