Ben*_*ash 4 iphone cocoa-touch objective-c uitableview uinavigationcontroller
我正在用UITableView构建一个iPhone应用程序.在选择一行时,我希望在加载特定URL时推送UIWebView.
我目前有:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
NSDictionary * story = [stories objectAtIndex:[indexPath row]]; 
NSString * storyLink = [NSString stringWithFormat:[story objectForKey:@"link"]];
    [[self navigationController] pushViewController:[[FirstViewController alloc] initWithNibName:@"FirstView" bundle:[NSBundle mainBundle]] animated:YES];
}
如何通过以下方式获取滑入的新视图以包含UIWebView并随后将故事链接加载到其中:
loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:storyLink]]
提前致谢,
石磊
正如其他人所说,一般的想法是在FirstViewController上有一个属性,它存储它需要加载的URL,然后在视图出现时将该URL加载到UIWebView中.
以下是一个示例,从标题开始:
@interface FirstViewController : UIViewController {
  UIWebView *webView;
  NSURL *storyURL;
}
@property (nonatomic, retain) IBOutlet UIWebView *webView; // this is set up in your nib
@property (nonatomic, retain) NSURL *storyURL;
@end
现在实施:
@implementation FirstViewController
@synthesize webView;
@synthesize storyURL;
- (void)dealloc;
{
  [storyURL release]; // don't forget to release this
  [super dealloc];
}
- (void)viewDidAppear:(BOOL)animated;
{
  // when the view appears, create a URL request from the storyURL
  // and load it into the web view
  NSURLRequest *request = [NSURLRequest requestWithURL:self.storyURL];
  [self.webView loadRequest:request];
}
- (void)viewWillDisappear:(BOOL)animated;
{
  // there is no point in continuing with the request load
  // if somebody leaves this screen before its finished
  [self.webView stopLoading];
}
@end
所以现在你需要在控制器中为前一个视图做的就是获取故事URL,将其传递给FirstViewController并推送它.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath  {
  NSDictionary *story = [stories objectAtIndex:[indexPath row]]; 
  NSURL *storyURL = [NSURL URLWithString:[story objectForKey:@"link"]];
  FirstViewController *firstViewController = [[FirstViewController alloc] initWithNibName:@"FirstView" bundle:[NSBundle mainBundle]]; // I'm pretty sure you don't need to pass in the main bundle here
  firstViewController.storyURL = storyURL;
  [self.navigationController pushViewController:firstViewController animated:YES];
  [firstViewController release]; // don't leak memory
}
这就是它.其他几点:
我假设你的字典中的"链接"值已经是一个字符串 - 如果是这种情况,在原始示例中构建一个全新的字符串是不必要的.正如您在我的示例中所看到的,我们可以直接使用此字符串来创建NSURL实例.
在原始代码中,当您分配/初始化FirstViewController时,您可以直接将其传递给pushViewController.一旦UINavigationController完成它就会产生内存泄漏(在它从导航堆栈弹出之后),它的保留计数仍然是1.至少你应该调用autorelease但是这里最有效的事情就是alloc/init它,将它存储在临时变量中,然后release在我们推送之后直接调用它.这可以确保一旦弹出导航堆栈,它就会被释放.
| 归档时间: | 
 | 
| 查看次数: | 18238 次 | 
| 最近记录: |