我正在尝试向我的应用程序添加一个“主页”按钮,如果您单击工具栏中的按钮,则会将您带到特定的网址“google.com”。
所以我可以在导航栏中放置一个按钮,并使其显示“主页”。
我可以将其放入 viewcontroller.m 中:
-(void)openGoogleURL
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];
}
我在 View Controller 中放入的内容正确吗?如何连接我的按钮?
最佳答案
UINavigationController
使用每个 UIViewController
的 UINavigationItem
对象来显示导航栏中的项目。因此,您可以通过在 View Controller 上执行以下操作来在栏中添加所需的按钮:
[self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Home" style:UIBarButtonItemStylePlain target:self action:@selector(openGoogleURL)]];
这样按钮目标操作将与您在 UIViewController 中定义的函数链接。
请注意,对于要添加此按钮的每个 View Controller ,您都需要此代码。
关于objective-c - xcode 导航按钮 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13953545/