iOS:创建链接按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6748473/
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
iOS: create a link button
提问by cyclingIsBetter
I want to create an IBAction for a button, that when you push it, the app go in background and at the same time open safari at a specific link (example "www.google.it") Can you help me?
我想为一个按钮创建一个 IBAction,当你按下它时,应用程序进入后台,同时在特定链接(例如“www.google.it”)上打开 safari 你能帮我吗?
回答by Patrick Perini
Inside your IBAction method, include the line
在您的 IBAction 方法中,包括以下行
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]];
回答by Daniel
If you use interface builder :
如果您使用界面构建器:
Create a UITextView and not a UIButton, write the link as text to textview and select the links checkbox in the interface builder. It will become the link you want at run time.
创建一个 UITextView 而不是 UIButton,将链接作为文本写入 textview 并选择界面构建器中的链接复选框。它将在运行时成为您想要的链接。
回答by cristallo
using swift
使用 swift
UIApplication.sharedApplication().openURL(NSURL(string: "http://www.google.com")!)
or swift 3.0
或快速 3.0
UIApplication.shared.openURL(URL(string: "http://google.com")!)
回答by deepa
@interface ViewController : UIViewController<NSURLConnectionDelegate>
@property (strong, nonatomic) IBOutlet UIButton *okbutton;
@property (strong, nonatomic) IBOutlet UIButton *canel;
@property (strong, nonatomic) IBOutlet UITextField *textfiled;
- (IBAction)okButtonClick:(id)sender;
- (IBAction)CanelButton:(id)sender;
- (IBAction)okButtonClick:(id)sender {
NSString *searchString = textfiled.text; // UItextField.text
NSString *finalString = [NSString stringWithFormat:@"http://www.google.com/search?q=%@",searchString];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:finalString]];
}