0

我正在尝试使用一个 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];
    }


}
4

2 回答 2

1

segue.destinationViewController返回一个类型的对象id。您需要将其转换为您的视图控制器类型以在没有警告的情况下发送消息:

[(YourViewController *)segue.destinationViewController setUrlNo:1];
于 2012-07-05T15:04:52.760 回答
0

要打开不同的网站并通过 segue 发送 url,您可以使用基于主视图控制器的此方法。您从每个表格单元格或按钮创建一个 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

祝你好运!

于 2013-04-04T09:46:19.163 回答