导航栏分段控制
我在视图控制器中添加了一个分段控件。我的 viewdidLoad 如下所示
self.navController = [[[UINavigationController alloc] init] autorelease];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Image", @""),
NSLocalizedString(@"Text", @""), nil]];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
segmentedControl.frame = CGRectMake(0, 0, 400, 30);
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
[self.view addSubview:self.navController.view];
[super viewDidLoad];
只有导航栏不断出现,而内部没有任何分段控件。 有人可以帮忙并让我知道这里到底出了什么问题吗?
I am adding a segmented control inside my view controller.My viewdidLoad is as follow
self.navController = [[[UINavigationController alloc] init] autorelease];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(@"Image", @""),
NSLocalizedString(@"Text", @""), nil]];
[segmentedControl setSelectedSegmentIndex:0];
[segmentedControl setAutoresizingMask:UIViewAutoresizingFlexibleWidth];
[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
segmentedControl.frame = CGRectMake(0, 0, 400, 30);
[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
[self.view addSubview:self.navController.view];
[super viewDidLoad];
Only the navigation bar keeps on coming without any segmented control inside it.
Can someone help and let me know what exactly is wrong here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的导航控制器一开始没有根视图控制器 - 您将分段控件正确设置为视图控制器的标题视图,但没有为导航控制器提供对该视图控制器的引用。您需要像这样初始化它:
Your navigation controller is starting out with no root view controller—you're setting the segmented control as your view controller’s title view correctly, but you’re not giving the navigation controller a reference to that view controller. You need to initialize it like this: