开发者

Make NSButton a Hyperlink in Cocoa

开发者 https://www.devze.com 2023-03-24 23:04 出处:网络
I was wondering how to make an NSButton a hyperlink so it opens an external webpage i开发者_StackOverflow社区n the user\'s default browser.

I was wondering how to make an NSButton a hyperlink so it opens an external webpage i开发者_StackOverflow社区n the user's default browser.

Thanks in advance!


-(IBAction)clicked:(id)sender {
    [[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"http://stackoverflow.com"]];
}


You cannot make an NSButton a hyperlink because a hyperlink is an HTML concept while a button is a Cocoa interface component.

But you can use a button's target/action to open a URL programmatically. The action could be a method like:

- (IBAction)openSomeURL:(id)sender
{
    NSURL *myURL = [NSURL URLWithString:@"http://google.com/"];
    [[NSWorkspace sharedWorkspace] openURL:myURL];
}


Also look and feel you want to appear button as hyperlink then remove the border of NSButton and use above two answer code for opening the url.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号