表视图 Segue 到另一个视图控制器



我是编程新手,可能挂在一个简单的问题上。我正在表视图中对我的数组使用解析。选择该行后,我想转到另一个视图控制器上的搜索栏。segue 工作正常,表视图工作正常,但我似乎无法让 objectId 通过。

#import "bookmarkViewController.h"
#import "Parse/Parse.h"
#import <ParseUI/ParseUI.h>
#import "ViewController.h"
@implementation bookmarkViewController
@synthesize postArray;

#pragma mark - View lifecycle
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc]                
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self      
action:@selector(refreshButtonHandler:)]];
}
- (void)viewWillAppear:(BOOL)animated
{
    if ([PFUser currentUser])
        [self refreshButtonHandler:nil];
}
#pragma mark - Button handlers
- (void)refreshButtonHandler:(id)sender
{
    //Create query for all Post object by the current user
    PFQuery *postQuery = [PFQuery queryWithClassName:@"Post"];
    [postQuery whereKey:@"author" equalTo:[PFUser currentUser]];
    // Run the query
    [postQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!error) {
            //Save results and update the table
            postArray = objects;
            [self.tableView reloadData];
        }
    }];
}
#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:     
(NSInteger)section
{
    // Return the number of rows in the section.
    return postArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:   
(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView   
dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault  
reuseIdentifier:CellIdentifier];
    }
    // Configure the cell with the textContent of the Post as the cell's text label
    PFObject *post = [postArray objectAtIndex:indexPath.row];
    [cell.textLabel setText:[post objectForKey:@"textContent"]];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath    
*)indexPath{
    NSLog(@"cell tapped");
    PFObject *post = [postArray objectAtIndex:indexPath.row];
    NSLog(@"%@", post.objectId);
    [self performSegueWithIdentifier:@"searchBookmark" sender:self];

}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    ViewController *vc = [segue destinationViewController];
    vc.labelText = post.objectId;    
  }
}

@end

在vc.label.text上,我总是使用未声明的标识符"post",但我似乎无法弄清楚如何识别它。 它就在上面的方法中。

NSLogs 正确回复 16:17:27.513 [应用] 单元格已点击 [App] cgdVY7Eu9h

将 didSelectRowAtIndexPath 和 prepareForSegue 更改为:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath* )indexPath{
    [self performSegueWithIdentifier:@"searchBookmark" sender:self];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
    NSLog(@"cell tapped");
    PFObject *post = [postArray objectAtIndex:indexPath.row];
    NSLog(@"%@", post.objectId);
    ViewController *vc = [segue destinationViewController];
    vc.labelText = post.objectId;    
    }
}

Post 是您在 didSelectRowAtIndexPath 中创建的局部变量,因此不能在该方法之外使用。解决此问题的简单方法是将 post 作为 execute SegueWithIdentifier:sender: 中的发送者参数传递。您可以将任何对象作为发件人传递。

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath {
    NSLog(@"cell tapped");
    PFObject *post = [postArray objectAtIndex:indexPath.row];
    NSLog(@"%@", post.objectId);
    [self performSegueWithIdentifier:@"searchBookmark" sender:post];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    PFObject *post = (PFObject *)sender;
    ViewController *vc = [segue destinationViewController];
    vc.labelText = post.objectId;    
}

UIViewController -> segue -> UITableViewController

有一个问题,我用答案解决了 -1- 谢谢。所以我有一种UIViewController,我想用按钮切换到另一个UITableViewController,我注意到它堆叠并被冻结了。我无法滚动我的表格...

我的代码是:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
MasterViewController *controller =segue.destinationViewController;
controller.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:controller animated:YES completion:nil];
} 

我的 CPU 过载超过 100%。所以答案 1 对我很有效。然后,新代码是:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    MasterViewController *vc = [segue destinationViewController];
    }

带有 30 个条目的表格现在就像一个魅力一样工作 =)

最新更新