使用SEGUES将数据从表传递到webview

时间:2012-01-29 21:01:27

标签: iphone ios5 uiwebview storyboard segue

我有一张基于 Sam自学iOS开发的 FlowerViewController 的表格,在 didSelectRowAtIndesPath 下,它会转到新笔尖的网站(我调整了部分传递数据)。 我的问题:我想更新这个,而不是去一个笔尖,在故事板中塞入。我知道不是使用 didSelectRow ... 我使用 prepareForSegue ...但我无法弄清楚细节......

我的ViewController.m包含以下内容:

- (void)viewDidLoad {
    [self movieData];
    [super viewDidLoad];

    self.title = @"Movies";

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}


#pragma mark -
#pragma mark Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [movieSections count];
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [[movieData objectAtIndex:section] count];
}


// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView 
         cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"Cell";

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] 
                 initWithStyle:UITableViewCellStyleSubtitle 
                 reuseIdentifier:CellIdentifier];
    }

    // Configure the cell.

    [[cell textLabel] 
     setText:[[[movieData 
                objectAtIndex:indexPath.section] 
               objectAtIndex: indexPath.row] 
              objectForKey:@"name"]];

    [[cell imageView] 
     setImage:[UIImage imageNamed:[[[movieData 
                                     objectAtIndex:indexPath.section] 
                                    objectAtIndex: indexPath.row] 
                                   objectForKey:@"picture"]]];

    [[cell detailTextLabel] 
     setText:[[[movieData 
                objectAtIndex:indexPath.section] 
               objectAtIndex: indexPath.row] 
              objectForKey:@"detail"]];
    cell.detailTextLabel.numberOfLines = 0;

    cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


// Override to support row selection in the table view.
- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    WebViewController *webViewController = 
    [[WebViewController alloc] initWithNibName:
     @"WebViewController" bundle:nil];

    webViewController.detailURL=
    [[NSURL alloc] initWithString: 
     [[[movieData objectAtIndex:indexPath.section] objectAtIndex: 
       indexPath.row] objectForKey:@"url"]];

    webViewController.title=
    [[[movieData objectAtIndex:indexPath.section] objectAtIndex: 
      indexPath.row] objectForKey:@"name"];

    [self.navigationController pushViewController:
     webViewController animated:YES];

}


#pragma mark -
#pragma mark Table view delegate



#pragma mark -
#pragma mark Memory management

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Relinquish ownership any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Relinquish ownership of anything that can be recreated in viewDidLoad or on demand.
    // For example: self.myOutlet = nil;
}


- (void)movieData {

    NSMutableArray *myMovies;

    movieSections=[[NSMutableArray alloc] initWithObjects:
                    @"Movies",nil];

    myMovies=[[NSMutableArray alloc] init];

    [myMovies addObject:[[NSMutableDictionary alloc]
                           initWithObjectsAndKeys:@"Movie1",@"name",
                           @"1.png",@"picture",
                           @"http://www.url1.com",@"url",@"Some information",@"detail",nil]];
    [myMovies addObject:[[NSMutableDictionary alloc]
                           initWithObjectsAndKeys:@"Movie2",@"name",
                           @"2.png",@"picture",
                           @"http://www.url2.com",@"url",@"Some information 2",@"detail",nil]];
    [myMovies addObject:[[NSMutableDictionary alloc]
                           initWithObjectsAndKeys:@"Movie3",@"name",
                           @"3.png",@"picture",
                           @"http://www.url3.com",@"url",@"Some information 3",@"detail",nil]];
    [myMovies addObject:[[NSMutableDictionary alloc]
                           initWithObjectsAndKeys:@"Movie4",@"name",
                           @"4.png",@"picture",
                           @"http://www.url4.com",@"url",@"Some information 4",@"detail",nil]];

    movieData=[[NSMutableArray alloc] initWithObjects:
                myMovies,nil];
}

我试图注释掉 didSelectRowAtIndexPath 并为segue添加以下内容,但是单元格突出显示并且没有任何反应(谢天谢地它没有冻结/崩溃,但没有任何正面的内容)

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    if ([[segue identifier] isEqualToString:@"movieSegue"]) {

        NSIndexPath *selectedRowIndex = [self.tableView indexPathForSelectedRow];
        WebViewSegue *_webViewSegue = [segue destinationViewController];
        _webViewSegue.detailURL =
        [[NSURL alloc] initWithString:[[[movieData objectAtIndex:selectedRowIndex.section] objectAtIndex:
                                         selectedRowIndex.row] objectForKey:@"url"]];
    }
}

然后我希望它传递给WebViewSegue

WebViewSegue.h:

@interface WebViewSegue : UIViewController  {
    IBOutlet UIWebView *detailWebView;
    NSURL   *detailURL;
    IBOutlet UIActivityIndicatorView *activity;
    NSTimer *timer;
}

@property (nonatomic, weak) NSURL *detailURL;
@property (nonatomic, weak) UIWebView *detailWebView;
@property (nonatomic, weak) UIActivityIndicatorView *activity;

@end

WebViewSegue.m:

@synthesize detailWebView =_detailWebView;
@synthesize detailURL = _detailURL;
@synthesize activity =_activity;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle


- (void)viewDidLoad {
    [super viewDidLoad];

    [detailWebView loadRequest:[NSURLRequest requestWithURL:detailURL]];

    timer = [NSTimer scheduledTimerWithTimeInterval:(1.0/2.0) 
                                             target:self 
                                           selector:@selector(tick) 
                                           userInfo:nil 
                                            repeats:YES];
}

-(void)tick {
    if (!detailWebView.loading) 
        [activity stopAnimating];
    else 
        [activity startAnimating];

}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)wevView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Cannot connect"
                                                    message:@"Please check your connection and try again" 
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
    [alert show];
}

@end

2 个答案:

答案 0 :(得分:0)

这是很多要消化的代码;你应该尝试简化。但如果我读得正确,你的基本方法似乎是正确的。

首先,在prepareForSegue:sender:上放置一个断点,并确保它被调用,并且标识符符合您的预期。

然后在viewDidLoad上放置一个断点,并确保在您认为它应该被调用时。

我会将loadRequest:拉出自己的方法,并在viewDidLoadsetDetailURL:中调用它。如果所有这些都在一个故事板中,setDetailURL:之后可能会调用viewDidLoad


编辑我所说的是prepareForSegue:sender:可能是正确的。您遇到的问题是在呈现的视图控制器中。

- (void)reloadWebView { // Pull the loadRequest: out...
    [self.detailWebView loadRequest:[NSURLRequest requestWithURL:self.detailURL]];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self reloadWebView];    // ...and call it both in viewDidLoad...
    ...
}

- (void)setDetailURL:(NSURL *)URL {
    [URL retain];
    [detailURL release];
    detailURL = URL;
    [self reloadWebView]; // ...and in setDetailURL:
}

另请注意,您的计时器没有理由。只需在reloadWebView中启用您的进度指示器,然后在webViewDidFinishLoadwebView:didFailLoadWithError:中将其关闭即可。您当前的方法使得无法取消分配此视图控制器,因为计时器会永久保留它。

答案 1 :(得分:0)

我已在网站上的另一篇文章中回答了您的问题。请参阅我的回答here

具体地说,关于如何将数据从表传递到下一个故事板segue,首先为下一个故事板segue(即目标视图控制器)中的数据创建属性。然后在表的prepareForSegue方法(源视图控制器)中设置该属性。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // needed if you have multiple segues
    if ([[segue identifier] isEqualToString:@"changeNameAndDate"])
    {
        [[segue destinationViewController] setDataProperty:self.tableData];
        // where dataProperty is the property in the designation view controller
        // and tableData is the data your are passing from the source
    {
}