簡體   English   中英

使用IBAction切換回主視圖控制器

[英]Using an IBAction to switch back to Main View Controller

我有一個具有許多視圖的應用程序,這些視圖可以通過主視圖中的多個自定義UIButton導航到。 主視圖名為iBMRViewController,並具有一些受歡迎的圖形,這些圖形以PNG圖像的形式通過界面生成器插入。 它還具有6個自定義UIButton,這些是我通過使用以下代碼創建的:

// This is the code which creates, and defines the properties of the 'Warning' button on the main view.
    UIButton *warningButton = [[UIButton buttonWithType:UIButtonTypeRoundedRect] retain];
    warningButton.frame = CGRectMake(225.0, 270.0, 60.0, 60.0);
    [warningButton setTitle:@"" forState:UIControlStateNormal];
    warningButton.backgroundColor = [UIColor clearColor];
    [warningButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal ];
    UIImage *warningButtonImageNormal = [UIImage imageNamed:@"Warning.png"];
    UIImage *warningStretchableButtonImageNormal = [warningButtonImageNormal stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [warningButton setBackgroundImage:warningStretchableButtonImageNormal forState:UIControlStateNormal];
    UIImage *warningButtonImagePressed = [UIImage imageNamed:@"whiteButton.png"];
    UIImage *warningStretchableButtonImagePressed = [warningButtonImagePressed stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    [warningButton setBackgroundImage:warningStretchableButtonImagePressed forState:UIControlStateHighlighted];
    [warningButton addTarget:self action:@selector(warningButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:warningButton];

現在,所有這些都可以正常運行,並且按鈕可以完美運行,顯然我也為它們設置了可以完美運行的動作。 在每個頁面上,我都有一個UINavigationBar和一個UINavigationItem,它是通過界面構建​​器設置的,並使用以下代碼將其帶回到我的主視圖;

//This is the code which opens up the new view when 'Begin' button is tapped.
-(IBAction)beginHomeButtonAction:(id)sender {
    iBMRViewController *controller = [[BeginView alloc] initWithNibName:@"iBMRViewController" bundle:nil];
    controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    [self presentModalViewController:controller animated:YES];
    [controller release];

這也起作用,但是,當我帶我回到“ iBMRViewController”時,它僅顯示通過接口構建器xib文件(即,歡迎png文件)設置的內容。 它不顯示我通過代碼添加的按鈕。

誰能告訴我我哪里出了問題? 將不勝感激。

謝謝

實際上, beginHomeButtonAction不會“將您帶回到主視圖”。 它創建一個具有新視圖的新視圖控制器,並進行顯示。 這是相同種類的控制器類,但不是該類的相同實例

要關閉視圖,並實際上將用戶帶回到主視圖,您必須從主視圖中取消顯示的視圖。 如何執行此操作取決於您的呈現方式,但是您可以嘗試popViewControllerAnimateddismissModalViewControllerAnimated (谷歌是你的朋友)

暫無
暫無

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

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