Iphone 如何在UIViewController中添加UINavigationBar?
我有一个Iphone 如何在UIViewController中添加UINavigationBar?,iphone,objective-c,uiviewcontroller,uinavigationbar,exc-bad-access,Iphone,Objective C,Uiviewcontroller,Uinavigationbar,Exc Bad Access,我有一个UIViewController类(假设它是XXX)。我通过代码将此视图控制器显示为modally XXX *xxx = [ [XXX alloc] init]; [self presentModalViewController:xxx animated:YES]; [xxx release]; 我想在XXX视图的顶部添加一个导航栏。所以我在XXX的loadView方法中使用了UINavigationBar对象 UINavigationBar *navBar = [ [UINaviga
UIViewController
类(假设它是XXX)。我通过代码将此视图控制器显示为modally
XXX *xxx = [ [XXX alloc] init];
[self presentModalViewController:xxx animated:YES];
[xxx release];
我想在XXX视图的顶部添加一个导航栏。所以我在XXX的loadView
方法中使用了UINavigationBar
对象
UINavigationBar *navBar = [ [UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:navBar];
[navBar release];
但是,它抛出了一个错误“EXC_BAD_ACCESS”。有什么帮助吗
谢谢选项1: 尝试从名为XXX的viewController的XIB添加导航栏 选项2: 添加一个UINavigationController并以模式显示它 替换您的代码:
XXX *xxx = [[XXX alloc] init];
[self presentModalViewController:xxx animated:YES];
[xxx release];
使用此代码:
XXX *xxx = [[XXX alloc] init];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:xxx];
[self presentModalViewController:navigation animated:YES];
[navigation release];
希望这对您有所帮助。您可以这样做:
XXX *xxx = [[XXX alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:xxx];
[self presentModalViewController:navigationController animated:YES];
[xxx release];
[navigationController release];
将代码替换为:
XXX *xxx = [[ [XXX alloc] init]autorelease];
[self presentModalViewController:xxx animated:YES];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:xxx];
[self presentModalViewController:navigation animated:YES];
[navigation release];
我认为它将解决您的“
EXC\u BAD\u ACCESS
”问题 您可以通过在视图顶部添加工具栏来尝试此操作。在许多情况下,我见过poping模态控制器,这是一个很好的解决方案。但如果您想从MODAL controller导航更多控制器,则应使用UINavigationController
尝试将Navigationbar添加到Xib中的XXX视图中,如果您使用导航,则必须推送它…即'必须使用pushViewController,而不是presentModalViewController。谢谢。。因此,如果不在此处创建UINavigationController,则无法添加UINavigationBar。我说得对吗?@Rajkanth:是的,据我所知,可能应该是这样。当您尝试以编程方式添加时,会得到“EXC\u BAD\u EXCESS”。