尝试以编程方式创建 rightBarButtonItem

发布于 2024-08-18 20:25:28 字数 371 浏览 6 评论 0原文

这似乎不起作用。我做错了什么?

-(void)awakeFromNib{
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showNewEventViewController)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    NSLog(@"awaked");
    [rightBarButtonItem release];
}

This doesn't seem to be working. What am i doing wrong?

-(void)awakeFromNib{
    UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(showNewEventViewController)];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;
    NSLog(@"awaked");
    [rightBarButtonItem release];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

琉璃梦幻 2024-08-25 20:25:28

我的猜测是,您将 UIBarButtonItem 添加到了错误的对象!
您需要将其添加到 rootViewController (而不是像您可能所做的那样添加到 UINavigationController

YourRootViewController *theRootController = [[YourRootViewController alloc] init];

UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController];

UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(dismiss)];    
theRootController.navigationItem.rightBarButtonItem = btnCancel

[navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:navContainer animated:YES];

my guess is, that you add the UIBarButtonItem to the wrong object!
you need to add it, to the rootViewController (instead to the UINavigationController, as you probably did)

YourRootViewController *theRootController = [[YourRootViewController alloc] init];

UINavigationController* navContainer = [[UINavigationController alloc] initWithRootViewController:theRootController];

UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(dismiss)];    
theRootController.navigationItem.rightBarButtonItem = btnCancel

[navContainer setModalTransitionStyle:UIModalTransitionStyleCrossDissolve];
[self presentModalViewController:navContainer animated:YES];
谜兔 2024-08-25 20:25:28

我通常会将此代码放在 viewDidLoad 方法中,而不是 awakeFromNib 方法中;我不确定这是否是你的问题所在。 “不工作”是什么意思?

I would normally put this code in the viewDidLoad method rather than the awakeFromNib method; I'm not sure if that's where your problem lies. What does "not working" mean?

水染的天色ゝ 2024-08-25 20:25:28

试试这个:

- (void) initUI {   
   UIBarButtonItem *btnCancel = [[[UIBarButtonItem alloc] initWithTitle:@"Cancel" 
                                  style:UIBarButtonItemStyleBordered 
                                  target:self 
                                  action:@selector(dismiss)]autorelease];    

   self.navigationItem.rightBarButtonItem = btnCancel;

   //[btnCancel release]; no need to explicitly release the item

}

Try this instead:

- (void) initUI {   
   UIBarButtonItem *btnCancel = [[[UIBarButtonItem alloc] initWithTitle:@"Cancel" 
                                  style:UIBarButtonItemStyleBordered 
                                  target:self 
                                  action:@selector(dismiss)]autorelease];    

   self.navigationItem.rightBarButtonItem = btnCancel;

   //[btnCancel release]; no need to explicitly release the item

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文