以编程方式创建按钮到URL

时间:2011-05-03 07:22:11

标签: cocoa-touch uibutton

我已经设法通过代码在Xcode中创建一个简单的圆角矩形按钮,但实际上让它做更多我想要的东西我有点麻烦。

理想情况是一种自定义按钮类型,图像处于两种状态,因为image.png链接到一个网址(例如www.google.com),任何人都可以帮助我吗?我认为我需要编写一个方法来进行链接。我可以处理,只是不知道如何在没有Interface Builder(目标iOS)的情况下将其连接起来

现有代码

           UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn addTarget:self action:@selector(supporturl) forControlEvents:UIControlEventTouchDown];
        btn.frame = CGRectMake(20, 20, 150, 50);
        [btn setTitle:@"Support and Help" forState:UIControlStateNormal];
        [self addSubview:btn];

1 个答案:

答案 0 :(得分:1)

UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 150.0f, 50.0f)];
[btn setTitle:@"Support and Help" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:@"image.png"] forState:UIControlStateNormal];
[btn addTarget:self action:@selector(openWebPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];

和功能

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