【问题标题】:Pass data from a table to a webview USING SEGUES使用 SEGUES 将数据从表传递到 web 视图
【发布时间】:2012-02-21 19:50:48
【问题描述】:

我有一个基于 Sam 的自学 iOS 开发 FlowerViewController 的表格,它在 didSelectRowAtIndesPath 下进入一个新 nib 中的网站(我调整了部分传递的数据)。 我的问题:我想更新这个,而不是去一个笔尖,在故事板中继续。我知道我没有使用 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

【问题讨论】:

    标签: iphone ios5 uiwebview storyboard segue


    【解决方案1】:

    我已在网站上的另一篇文章中回答了您的问题。看我的回答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
        {
    }
    

    【讨论】:

    • 感谢您的尝试,但我认为您的回答是关于如何处理笔尖;然而,我的问题是关于将数据从表传递到下一个故事板 segue。对代码还有其他想法吗??
    【解决方案2】:

    要消化很多代码;你应该尽量简化。但如果我没看错的话,你的基本方法似乎是正确的。

    首先,在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: 中将其关闭。您当前的方法无法释放此视图控制器,因为计时器会永久保留它。

    【讨论】:

    • 谢谢你说我的尝试可能是正确的......但是你可以尝试一下代码来填充prepareForSegue吗?谢谢!我真的不确定将 loadRequest 拉入它自己的方法是什么意思...不确定我会在该方法中放入什么...
    • @ms 您是否按照@rob 的建议设置了断点?结果如何。
    猜你喜欢
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-10
    • 1970-01-01
    • 2013-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多