用于启动 URL 的 Xcode 按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11528950/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 04:29:30  来源:igfitidea点击:

Xcode Button to Launch URL

xcodebutton

提问by user1532706

I've created a simple app that has a button in the bottom toolbar and I can't seem to get the button to work. I've tried to attach an action to it that will open a URL when pressed but it did not work. So I've removed that action and I'm hoping someone can help. I've posted the compressed xcode project at this URL https://www.box.com/s/5d45ce1df7d9dd0fe205

我创建了一个简单的应用程序,在底部工具栏中有一个按钮,但我似乎无法让该按钮工作。我试图给它附加一个动作,当按下时它会打开一个 URL,但它没有用。因此,我已删除该操作,希望有人可以提供帮助。我已经在这个 URL https://www.box.com/s/5d45ce1df7d9dd0fe205 上发布了压缩的 xcode 项目

Any help is appreciated.

任何帮助表示赞赏。

回答by Anthony

Something like this would work. This is an example of a text-only button:

像这样的事情会起作用。这是纯文本按钮的示例:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 100, 40)];
[button setBackgroundColor:[UIColor clearColor]];
[button setTitle:@"Google" forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[button addTarget:self action:@selector(openGoogleURL) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

And the button's selector:

和按钮的选择器:

-(void)openGoogleURL
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://www.google.com"]];
}

回答by changtung

Use another viewcontroller with webView embeeded. This solution force to stay in your application during watching url content.

使用另一个嵌入了 webView 的视图控制器。此解决方案强制在观看 url 内容期间留在您的应用程序中。

  1. Create another view controller on storyboard.
  2. place webview on it. ( name it webView )
  3. connect web view with controller class through assistant editor ( assuming that You connected new UIView class to view controller )
  4. in viewDidLoad of your new class insert:

     [super viewDidLoad];
     NSString *fullURL = @"http://google.pl";
    
     NSURL *url = [NSURL URLWithString:fullURL];
    
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:requestObj];
    
  1. 在情节提要上创建另一个视图控制器。
  2. 将 webview 放在上面。(将其命名为 webView )
  3. 通过辅助编辑器将 web 视图与控制器类连接(假设您将新的 UIView 类连接到视图控制器)
  4. 在新类的 viewDidLoad 中插入:

     [super viewDidLoad];
     NSString *fullURL = @"http://google.pl";
    
     NSURL *url = [NSURL URLWithString:fullURL];
    
     NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
    
    [self.webView loadRequest:requestObj];