我将在我的应用程序中添加一个指向网站的链接。用户将点击一个名为“网站”的按钮,然后会出现一个警报,其中包含两个按钮。其中一个按钮只是一个取消按钮,另一个按钮将打开网站。
您需要帮助我吗?
谢谢!
您需要帮助我吗?
谢谢!
将此内容放到您的头文件中:
@interface YourViewController : UIViewController <UIAlertViewDelegate>
将以下内容放入包含警报的类中:
- (void)alertOKCancelAction {
// open a alert with an OK and cancel button
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Open?" message:@"Open Website?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Open", nil];
alert.tag = 1;
[alert show];
[alert release];
}
添加此方法:
- (void)alertView:(UIAlertView *)alert clickedButtonAtIndex:(NSInteger)buttonIndex
{
// the user clicked one of the OK/Cancel buttons
if(alert.tag == 1)
{
if(buttonIndex == alert.cancelButtonIndex)
{
NSLog(@"cancel");
}
else
{
NSLog(@"ok");
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: @"http://www.google.com"]];
}
}
}