在Xcode上的导航栏左侧添加一个按钮

时间:2012-08-13 06:13:41

标签: xcode button uinavigationbar ios5.1

我想在导航栏的左侧创建一个按钮,点击它后,用户将被带入另一个页面。但是我无法使它发挥作用。以下是我的代码。请帮忙!

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelEdit:)];

2 个答案:

答案 0 :(得分:3)

试试这个。

UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]
                                initWithTitle:@"Left"
                                style:UIBarButtonItemStyleBordered
                                target:self
                                action:@selector(leftItemAction)];
UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]
                               initWithTitle:@"Right"
                               style:UIBarButtonItemStyleBordered
                               target:self
                               action:@selector(rightItemAction)];
self.navigationItem.rightBarButtonItem = rightItem;
self.navigationItem.leftBarButtonItem = leftItem;

- (void)leftItemAction
{
...
}

- (void)rightItemAction
{
...
}

答案 1 :(得分:1)

我已使用此代码添加保存按钮

UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
[button2 setBackgroundImage:[UIImage imageNamed:@"save.png"] forState:UIControlStateNormal];
button2.frame = CGRectMake(0, 0, 100, 31);
[button2 addTarget:self action:@selector(onSave:) forControlEvents:UIControlEventTouchUpInside];    
saveButton = [[UIBarButtonItem alloc]initWithCustomView:button2];
self.navigationItem.leftBarButtonItem = saveButton;