具有多个segues的UITableViewController进入错误的视图

时间:2014-01-20 05:00:08

标签: objective-c ios7 uitableview xcode5 uistoryboardsegue

所以我完全被这里难住了。我正在使用xcode 5,我有一个带有2个segues的表视图。我试着用两个不同的原型单元设置segue,但这不起作用。所以我现在在UITableViewController上设置了两个segue。似乎我最后创造的那个segue就是它所追求的那个。我没有收到任何错误,当我单步执行代码时,它会触发正确的行来执行segue。

这似乎每次都完美地执行,但它只是没有找到正确的视图。

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if([currentTableViewType isEqualToString:@"feedbackOnly"]){
        // hits this line correctly
        [self performSegueWithIdentifier:@"ShowSendFeedback" sender:indexPath];
    } else {
        // hits this line correctly
        [self performSegueWithIdentifier:@"ShowTicketDetails" sender:indexPath];
    }
}
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSIndexPath *)selectedIndexPath {
    if ([[segue identifier] isEqualToString:@"ShowTicketDetails"])
    {
        TicketDetailsViewController *tdv = [segue destinationViewController];
        NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
        long row = [myIndexPath row];
        Ticket * currentTicket = [ticketsArray objectAtIndex:row];
        tdv.tPriority = currentTicket.ticketPriority;
        tdv.tId = currentTicket.ticketId;
        tdv.tStatus = currentTicket.ticketStatus;
    }

    if ([[segue identifier] isEqualToString:@"ShowSendFeedback"]){
        FeedbackViewController *fvc = [segue destinationViewController];
        NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
        long row = [myIndexPath row];
        Ticket * currentTicket = [ticketsArray objectAtIndex:row];
        fvc.tPriority = currentTicket.ticketPriority;
        fvc.tId = currentTicket.ticketId;
        fvc.tStatus = currentTicket.ticketStatus;
    }
}

为了彻底,我已经把我的整个控制器都包括在内了。

#import "TicketsViewController.h"
@interface TicketsViewController ()
@end
@implementation TicketsViewController
@synthesize json, ticketsArray, ticketsTableView, currentTableViewType;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Tickets";

    AppDataClass *appData=[AppDataClass getInstance];
    currentTableViewType = appData.tableViewType;
    [self retrieveData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return ticketsArray.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if([currentTableViewType isEqualToString:@"feedbackOnly"]){
        return @"Requires Feedback";
    } else {
        return @"Issues";
    }
}

- (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];
    }

    Ticket * currentTicket = [ticketsArray objectAtIndex:indexPath.row];

    cell.textLabel.text = currentTicket.ticketName;
    cell.detailTextLabel.text = currentTicket.ticketAddress;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    if([currentTicket.ticketPriority.lowercaseString isEqualToString:@"high"]) {
        cell.imageView.image = [UIImage imageNamed:@"alert.png"];
        if([indexPath row] % 2) [cell setBackgroundColor:[UIColor lightGrayColor]];
    }
    return cell;
}

#pragma mark - Methods
- (void)retrieveData
{
   // NSURL * url = [NSURL URLWithString:getDataURL];
   // NSData * data = [NSData dataWithContentsOfURL:url];
    AppDataClass *appData=[AppDataClass getInstance];
    NSString *args = @"userid=%@&authid=%@";
    NSString *values=[NSString stringWithFormat:args, appData.userId, appData.authId];
    json = [appData getPostData:appData.URL_LIST_TICKETS:values];

    ticketsArray = [[NSMutableArray alloc] init];

    // segeue names
    // allTickets
    // feedbackOnly

    for(int i=0; i<json.count; i++){
        NSString * tId = [[json objectAtIndex:i] objectForKey:@"id"];
        NSString * tName = [[json objectAtIndex:i] objectForKey:@"name"];
        NSString * tPriority = [[json objectAtIndex:i] objectForKey:@"priority"];
        Ticket * myTicket = [[Ticket alloc] initWithTicketId:tId andTicketName:tName andTicketPriority:tPriority];
        [ticketsArray addObject:myTicket];
    }

    [self.ticketsTableView reloadData];

}

- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if([currentTableViewType isEqualToString:@"feedbackOnly"]){
        [self performSegueWithIdentifier:@"ShowSendFeedback" sender:indexPath];
    } else {
        [self performSegueWithIdentifier:@"ShowTicketDetails" sender:indexPath];
    }
}

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(NSIndexPath *)selectedIndexPath {
    if ([[segue identifier] isEqualToString:@"ShowTicketDetails"])
    {
        TicketDetailsViewController *tdv = [segue destinationViewController];
        NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
        long row = [myIndexPath row];
        Ticket * currentTicket = [ticketsArray objectAtIndex:row];
        tdv.tPriority = currentTicket.ticketPriority;
        tdv.tId = currentTicket.ticketId;
        tdv.tStatus = currentTicket.ticketStatus;
    }

    if ([[segue identifier] isEqualToString:@"ShowSendFeedback"]){
        FeedbackViewController *fvc = [segue destinationViewController];
        NSIndexPath *myIndexPath = [self.tableView indexPathForSelectedRow];
        long row = [myIndexPath row];
        Ticket * currentTicket = [ticketsArray objectAtIndex:row];
        fvc.tPriority = currentTicket.ticketPriority;
        fvc.tId = currentTicket.ticketId;
        fvc.tStatus = currentTicket.ticketStatus;
    }
}

@end

1 个答案:

答案 0 :(得分:0)

好的......所以它似乎是XCode中的一个错误。我最初复制了第二个ViewController,我试图将其转换为并改变了它的类以及一些视图组件。我认为StoryBoard足够聪明,可以将此视图重新分配为自己的视图。我错了。 XCode似乎并不喜欢这样。从头开始使用新的ViewController似乎可以解决所有问题,现在segue正常工作。完全相同的代码有效。

因此,如果有人遇到此问题,请确保您没有复制并粘贴您尝试转移的ViewController。这将为您节省数小时的头痛: - )

它可能引用了相同的ViewController,这就是为什么我设置的最后一个segue是唯一有效的。