2012-02-28 131 views
0

我遇到了SettingsViewController上没有显示的后退按钮问题。当推送视图时,导航栏显示,但没有后退按钮。iPhone导航返回按钮

我在一个视图控制器里面创建了这个,它不是一个导航控制器。对这里实际发生的任何想法或建议。

- (void)viewDidLoad 
{ 
    self.title = @"Settings"; 
} 

- (IBAction)showSettingsModal:(id)sender 
{  
    SettingsViewController *settingsViewController = [[SettingsViewController alloc] initWithNibName:@"SettingsViewController" bundle:nil]; 
    UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:settingsViewController] autorelease]; 

    [self presentModalViewController:navController animated:YES]; 
    [settingsViewController release];  
} 

回答

3

您正在创建新的导航堆栈。您需要添加自己的“后退”按钮并将其操作设置为调用VC上的委托方法以解除它。

UPDATE: 似乎有很多的困惑在哪里以及如何解雇ModalViewControllers。在大多数情况下,错误的做法是从Modal VC本身调用Dismiss方法,如果您希望父母在解雇时采取行动。相反,使用委派。下面是一个简单的例子:

ModalViewController.h:

@protocol ModalViewControllerDelegate 
-(void)dismissMyModalVC; 
@end 


@interface ModalViewController : UIViewController { 
id <ModalViewControllerDelegate> delegate; 
} 

@property (nonatomic, retain) id <ModalViewControllerDelegate> delegate; 
// The rest of your class properties, methods here 

ModalViewController.m

@synthesize delegate; 

...

// Put in the Method you will be calling from that Back button you created 
[delegate dismissMyModalVC]; 

CallingViewController.h:

#import "ModalViewController.h" 

@interface CallingViewController : UIViewController 
<ModalViewControllerDelegate> 
// Rest of class here 

CallingViewController.m:

ModalViewController *mvc = [[ModalViewController alloc] initWithNibName:@"ModalViewController" bundle:nil]; 
mvc.delegate = self 
[self presentModalViewController:mvc animated:YES]; 

...

// The ModalViewController delegate method 
-(void)dismissMyModalVC { 
// Dismiss the ModalViewController that we instantiated earlier 
[self dismissModalViewControllerAnimated:YES]; 

这样的VC被从实例化它的控制器正确关闭。该委托方法可以修改通过沿对象以及

+0

我花了一些时间来实现,但这是正确的解决方案。 – Vikings 2012-04-28 21:19:37

0

您呈现新的控制器,模式视图控制器(当您完成登录用户,等等等等)。 Modal控制器呈现其最高级。你应该:

[self.navigationController pushViewController:navController animated:YES]; 

推视图控制器到堆栈中,然后你会看到后退按钮。

阅读呈现视图控制器苹果documenation: https://developer.apple.com/library/ios/#featuredarticles/ViewControllerPGforiPhoneOS/ModalViewControllers/ModalViewControllers.html

编辑没有看到调用视图控制器不是导航控制器的一部分。在这种情况下,你将不得不手动创建后退按钮,并将其设置为左栏导航项目。

+0

调用VC不是导航控制器的一部分(请参阅OP) – 2012-02-28 14:03:16

+0

这不起作用。我不明白self.navigationController,因为这发生在视图不是一个导航控制器。 – Vikings 2012-02-28 14:09:12

+0

我更新了我的答案,对不起。 – Maggie 2012-02-28 14:09:48

1

SettingsViewController没有后退按钮,因为它位于堆栈的底部。如果你想要一个按钮来消除模态对话框,你将不得不自己添加它。

1

你可以试试这个

UIBarButtonItem * backButton = [[UIBarButtonItem alloc]initWithTitle:@"Back"style:UIBarButtonItemStylePlain target:self.navigationItem.backBarButtonItem action:@selector(dismissModalViewControllerAnimated:)]; 
+0

小心 - 您几乎总是想要在呈现的VC类中调用委托方法来关闭模态VC,而不是在模态VC本身上调用dismissModalViewController。 – 2012-02-28 14:11:09

+0

@EJJay是对的,这最终导致我的应用程序出现问题 – Vikings 2012-04-28 21:18:41