segue to a view from a tableview in different class

匆匆过客 提交于 2020-01-05 12:32:13

问题


To explain the issue, I had ViewControllerA with a UITableView called commentTableView inside of it. and from commentTableView i would segue to ViewControllerB but I needed to add an addition UITableView called mentionedFriendTable to ViewControllerA. But once i did add mentionFriendTable I started to have issues with the tables that caused the app to crash.

Issue Im Having

I decided to place commentTableView into a different class called justCommentsTable which is a UITableViewController class and add that class to ViewController. I have to place commentTableView in a different class and not mentionFriendTable because mentionFriendTable needs to be in ViewControllerA. and after a couple of hours I finally got that to work. but now that segue I initially had to ViewControllerB does not work, and crashes saying

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Receiver (<justCommentsTable: 0x21032160>) has no segue with identifier 'segueToViewControllerB'

My Storyboard

On my storyboard i have ViewControllerA with a tableView inside of it and i linked up the tableview to @property (strong, nonatomic) IBOutlet UITableView *commentTable;
in the code below I explain how commentTable becomes the tableview from justCommentsTable

I know I'm getting this crash because ViewControllerB is connected to ViewControllerA through MainStoryboard.storyboard and not justCommentsTable

My Question

Is there a way to still segue ViewControllerA to ViewControllerB and pass data from commentTableView which is in a different class.

I'm gonna go ahead and place whatever code I find relevant to the issue. Just tell me if im missing any crucial code.

ViewControllerA.h

#import "justCommentsTable.h"

@interface ViewControllerA : UIViewController<UITableViewDelegate, UITableViewDataSource>{


       // in justCommentsTable.m I add this controller to the view
            justCommentsTable *commentsController;


}


      //   this is the table that is link up in storyboard
        @property (strong, nonatomic) IBOutlet UITableView *commentTable;

      //this is my mentions table that needs to be in ViewControllerA
        @property (nonatomic, strong) UITableView *mentionTableView;

@end

ViewControllerA.m

- (void)viewDidLoad
{

[super viewDidLoad];

        **// i set up the mentionTable here**        

self.mentionTableView.transform = transform;
self.mentionTableView.delegate = self;
self.mentionTableView.dataSource = self;
self.mentionTableView.tag = 1;

[self.view addSubview:self.mentionTableView];

        **// i set up the commentTable here**        

if (commentsController == nil) {
        commentsController = [[justCommentsTable alloc] init];
    }


    [commentTable setDataSource:commentsController];
    [commentTable setDelegate:commentsController];
    [commentsController setHomeUserID:homeUserID];
    [commentsController setGetEventHostIDforNotif:getEventHostIDforNotif];
    [commentsController setGeteventIDfrSEgue:geteventIDfrSEgue];
    [commentsController setGetEVENTNamefrSegue:getEVENTNamefrSegue];

     **// i set commentsController to the Tableview in justCommentsTable class**        

            commentsController.view = commentsController.tableView;

}

justCommentsTable.h

@interface justCommentsTable : UITableViewController<UITableViewDelegate, UITableViewDataSource,UITextFieldDelegate,UITextViewDelegate>

@end

justCommentsTable.m

@implementation justCommentsTable
   //buttonTag is used for a button on the customCells
int buttonTag;

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *CellIdentifier = @"commentCell";

    customCell *cell =(customCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[customCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    NSDictionary *commmentDict = [CommentArray objectAtIndex:indexPath.row];
    NSString *commentText = [commmentDict objectForKey:@"comment"];


    cell.textLabel.text = commentText;

      // i need a custom button on the cells for other reasons
    [cell.cellButton addTarget:self action:@selector(showButtonIndex:) forControlEvents:UIControlEventTouchUpInside];

 return cell;
}



-(void)showButtonIndex:(UIButton*)button{
    buttonTag = button.tag;
     /*
     buttonTag gets set here, and used in the prepareForSegue method
      to find out what row the button was on.
      */

    [self performSegueWithIdentifier:@"segueToViewControllerB" sender:self];

}


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

if ([segue.identifier isEqualToString:@"segueToViewControllerB"]){
    //segue to profile from image button
    ViewControllerB *VCB = segue.destinationViewController;

    NSDictionary *commentDic = [CommentArray objectAtIndex:buttonTag];
     /*
          buttonTag was used to select the objectAtIndex
      */

   NSString *commentTitle = [commentDic objectForKey:@"comment_title"];

    VCB.title = commentTitle;
   }
}

I know where my issue is, I just have no idea on how to fix it. and Ive searched for problems like mine, but I cant find one with a solid answer. if anyone can help, it would greatly be appreciated. thanks!

来源:https://stackoverflow.com/questions/18236516/segue-to-a-view-from-a-tableview-in-different-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!