2011-12-11 78 views
0

我正在寻找更快的方法来构建我的应用程序。由于我有一系列的按钮,他们将打开一个UIWebView。但每个按钮都会去不同的网站。多个UIButton打开相同的视图,但不同的内容

我可以知道是否有可能我只生成一个xib文件?或者我应该为每个按钮创建一个新文件?

这是我去下一个xib的代码。

- (IBAction)items:(id)sender { 

     //toggle the correct view to be visible 
     Chelseapic *myView1 =[[Chelseapic alloc] initWithNibName:nil bundle:nil]; 
     [myView1 setModalTransitionStyle:UIModalTransitionStylePartialCurl]; 
     [self presentModalViewController:myView1 animated:YES]; 
    } 

这是在WebView xib中打开URL的代码。

- (void)viewDidLoad 
{  
    [super viewDidLoad]; 
    NSString *urlAddress = @"http://www.google.com" ; 
    NSURL *url = [NSURL URLWithString:urlAddress]; 
    NSURLRequest *requestObj = [NSURLRequest requestWithURL:url]; 
    [webView loadRequest:requestObj]; 
    [self.view addSubview:webView]; 

} 

非常感谢

回答

1

如果你有一个固定的,你想在你的笔尖来布置按钮,尝试制作一个单独的IBAction为每个按钮:

// helper method 
- (void)presentViewControllerForURL:(NSURL *)url 
{ 
    Chelseapic *vc = [[Chelseapic alloc] initWithNibName:nil bundle:nil]; 
    vc.url = url; 
    [vc setModalTransitionStyle:UIModalTransitionStylePartialCurl]; 
    [self presentModalViewController:vc animated:YES]; 
} 

// Connect the first button in your nib to this action. 
- (IBAction)presentGoogle:(id)sender 
{ 
    [self presentViewControllerForURL:[NSURL URLWithString:@"http://www.google.com/"]]; 
} 

// Connect the second button in your nib to this action. 
- (IBAction)presentStackOverflow:(id)sender 
{ 
    [self presentViewControllerForURL:[NSURL URLWithString:@"http://stackoverflow.com/"]]; 
} 

// etc. 

你需要提供Chelseapic a url它在viewDidLoad中使用的属性,而不是在那里对URL进行硬编码。

+0

谢谢你的帮助。它正在为我节省很多时间。 – Clarence

相关问题