2011-07-19 81 views
24

我想創建一個按鈕的IBAction,當你按下它時,應用程序會在後臺運行,同時打開特定鏈接的Safari瀏覽器(例如「www.google.it」 ) 你能幫我嗎?iOS:創建一個鏈接按鈕

回答

59

裏面你IBAction爲方法,包括行

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.google.com"]]; 
3

如果您使用Interface Builder:

創建一個UITextView,而不是一個UIButton,寫的聯繫,文本TextView的和選擇鏈接複選框界面生成器。它將在運行時成爲你想要的鏈接。

0
@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]]; 



} 
2

使用迅速

UIApplication.sharedApplication().openURL(NSURL(string: "http://www.google.com")!) 

或迅速3.0

UIApplication.shared.openURL(URL(string: "http://google.com")!)