我正在尝试使用一个Web视图,并使用三个不同的按钮将您带到此Web视图。每个人都会带你到另一个网站。我已经创建了一个新的webview类,在这个类中我有一个方法来设置它将去往哪个url。
在原始视图控制器中,我正在尝试向此方法发送不同的数字,但它表示没有已知的实例方法。
代码如下:
设置网址的方法
-(void)setUrlNo:(int)urlNo
{
if (urlNo == 0) {
url = @"http://www.twitter.com/ac3112";
} else if (urlNo == 1) {
url = @"http://www.facebook.com/ac3112";
} else if (urlNo == 2) {
url = @"http://www.adamcarlin.co.uk";
}
}
准备segue方法
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"Twitter"])
{
[segue.destinationViewController setUrlNo:1];
}else if ([segue.identifier isEqualToString:@"FB"]) {
[segue.destinationViewController setUrlNo:2];
} else {
[segue.destinationViewController setUrlNo:3];
}
}
答案 0 :(得分:1)
segue.destinationViewController
返回id
类型的对象。您需要将其强制转换为视图控制器类型,以便在没有警告的情况下发送消息:
[(YourViewController *)segue.destinationViewController setUrlNo:1];
答案 1 :(得分:0)
要打开不同的网站并通过segue发送网址,您可以使用基于主视图控制器的此方法。您可以从每个表格单元格或按钮创建一个push segue并拖动到UIViewController,然后将下面的segue值放在那里以确保您获得正确的站点/值。
FollowViewController.h
@interface FollowViewController : UIViewController
@end
FollowViewController.m segue方法
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
FollowDetailViewController *webController = [[FollowDetailViewController alloc] init];
if ([[segue identifier] isEqualToString:@"Facebook"]) { // Facebook
NSString *urlstr=@"https://www.facebook.com/";
webController = [segue destinationViewController];
webController.urlstr = urlstr;
webController.title = @"Facebook"; // this sets the title of the next page
}
else if ([[segue identifier] isEqualToString:@"Instagram"]) { // Instagram
NSString *urlstr=@"http://instagram.com/";
webController = [segue destinationViewController];
webController.urlstr = urlstr;
webController.title = @"Instagram"; // this sets the title of the next page
}
else if ([[segue identifier] isEqualToString:@"Twitter"]) { // Twitter
NSString *urlstr=@"https://twitter.com/";
webController = [segue destinationViewController];
webController.urlstr = urlstr;
webController.title = @"Twitter"; // this sets the title of the next page
}
else if ([[segue identifier] isEqualToString:@"YouTube"]) { // YouTube
NSString *urlstr=@"http://www.youtube.com/";
webController = [segue destinationViewController];
webController.urlstr = urlstr;
webController.title = @"YouTube"; // this sets the title of the next page
}
self.title = @"Follow"; // This sets the title of the back button, and the title of this page
}
FollowDetailViewController.h
@interface FollowDetailViewController : UIViewController {
NSString *urlstr;
}
@property (strong, nonatomic) IBOutlet UIWebView *WebView;
@property (strong, nonatomic) NSString *urlstr;
@end
FollowDetailViewController.m
@implementation FollowDetailViewController
@synthesize WebView;
@synthesize urlstr;
- (void)viewDidLoad
{
[super viewDidLoad];
NSURL *url = [NSURL URLWithString:urlstr];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[self.WebView loadRequest:requestObj];
}
@end
祝你好运!