objective-c 将左按钮添加到 UINavigationBar (iPhone)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/565989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding left button to UINavigationBar (iPhone)
提问by 4thSpace
I've created a new navigation based iPhone app. I added this to the RootViewController.
我创建了一个新的基于导航的 iPhone 应用程序。我将此添加到 RootViewController。
- (void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] init];
self.navigationItem.leftBarButtonItem = addButton;
self.navigationItem.leftBarButtonItem.enabled = YES;
}
No left button displays however. Is there something I need to do?
但是没有左键显示。有什么我需要做的吗?
回答by Stephen Darlington
You don't define what the button actually does. This is a line from my app:
您没有定义按钮的实际作用。这是我的应用程序中的一行:
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelEdit:)];
cancelEdit:, the selector, is in the current class (self) and is defined as:
cancelEdit:,选择器,在当前类(self)中,定义为:
- (void) cancelEdit: (id) sender;
回答by Roman B.
There is actually another answer, which is not listed here, but might be very useful in many cases.
If you do not want to use UINavigationController, then self.navigationItemis not an option for you.
实际上还有另一个答案,此处未列出,但在许多情况下可能非常有用。如果您不想使用 UINavigationController,self.navigationItem则不适合您。
UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"imageName"] style:UIBarButtonItemStyleBordered target:self action:@selector(action:)];
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:@"Bar Title"];
navigationItem.leftBarButtonItem = barButton;
[navigationBar pushNavigationItem:navigationItem animated:NO];
You might want that when creating lightweight UIViewController with bar and buttons, but do not want navigation overhead.
在创建带有栏和按钮的轻量级 UIViewController 时,您可能希望这样做,但不希望导航开销。
回答by inked
On this question:
关于这个问题:
Awesome, thanks. Where do you find the various selectors available? The doc is very vague about this. I defined an instance method and put that as the selector but it was never executed. I'd like to slide in a detailview when the button is clicked. – 4thSpace Feb 19 at 16:19
很好,谢谢。您在哪里可以找到各种可用的选择器?文档对此非常含糊。我定义了一个实例方法并将其作为选择器,但它从未执行过。单击按钮时,我想在详细视图中滑动。– 4thSpace 2 月 19 日 16:19
I go the place where I need more information and press the escape (Esc) key. So, in this example:
我去需要更多信息的地方,然后按退出 (Esc) 键。所以,在这个例子中:
...(beginning of line)... @selector(Place Cursor Here, press Esc) ...
...(beginning of line)... @selector(Place Cursor Here, press Esc) ...
A list of the available selectors will appear. For Microsoft programmers, this is like Intellisense, but you have to ask for it with Esc (it just doesn't appear automatically like in Visual Studio). Practically speaking, XCode does create most of whatever you're trying to create when you start typing and it really helps when you figure out the Tab key is your friend. (well... it's myfriend... having the lonely life I have)
将出现可用选择器的列表。对于微软程序员来说,这就像 Intellisense,但你必须用 Esc 来请求它(它只是不像在 Visual Studio 中那样自动出现)。实际上,当您开始打字时,XCode 确实会创建您尝试创建的大部分内容,当您发现 Tab 键是您的朋友时,它真的很有帮助。(嗯……这是我的朋友……过着我孤独的生活)
Now, if you need your own selector, you can place your label in there (mySelector: for example), then, further down in your code, build it:
现在,如果你需要你自己的选择器,你可以把你的标签放在那里(例如mySelector),然后,在你的代码中进一步构建它:
- (IBAction)mySelector:(id)sender
{
NSLog(@"You touched me THERE!");
}
- (IBAction)mySelector:(id)sender
{
NSLog(@"You touched me THERE!");
}
Also, in the header (.h) file, be sure to put a corresponding:
另外,在头(.h)文件中,一定要放一个对应的:
-(IBAction) mySelector:(id)sender;
-(IBAction) mySelector:(id)sender;

