2012-08-13 108 views
0

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

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

回答

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; 
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 
{ 
... 
}