How to access an instance of a viewcontroller

Discussion in 'iOS Programming' started by LT21j, Apr 30, 2013.

  1. LT21j, Apr 30, 2013
    Last edited by a moderator: May 2, 2013

    LT21j macrumors newbie

    Joined:
    Apr 8, 2013
    #1
    I have a problem with an app I am creating. FYI I am a newbie and this is my first major project so I might be missing some basic mechanics/ information. But here it is, I am wondering how I can access the instance of the view controller thats created automatically in storyboard via segue. The basic design of the app is that I have a main view but then the user presses a button to go into an information view. This information view contains a table view which contains dynamic table cells. Each cell has a two labels and a button. The way I want to have it is when the user presses the button that I have connected to the custom table view cell class I want it to get the information and then segue to another view controller called schedule view. The problem I am having is that I don't know how to get the instance of of the information view that is automatically created. I'm trying to use the method [self performSegueWithIdentifier:mad:"toSchedule" sender: self];


    Any help would be much appreciated.

    [EDIT]
    Here is my code:
    Code:
    #import "TeacherViewController.h"
    #import "SchoolStaff.h"
    #import "ClassViewCell.h"
    
    @interface TeacherViewController ()
    
    @end
    
    @implementation TeacherViewController
    
    -(IBAction)returned:(UIStoryboard *) segue
    {
        //what the heck
    }
    
    - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
        // Return the number of sections.
        return 1;
    }
    
    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
        return _classList.count;
    }
    
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *CellIdentifier = @"classViewCell";
        ClassViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
        int row = [indexPath row];
        cell.Period.text = _classList[row][0];
        cell.cName.text = _classList[row][1];
        return cell;
    }
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    	// Do any additional setup after loading the view
        _datTeacher = [[[SchoolStaff sharing]fList]objectAtIndex:2];
        
        _freePeriods.text = _datTeacher.freePeriods;
        _classesTaught.text = _datTeacher.classesTaught;
        _teacherName.text = _datTeacher.tname;
        _department.text = [[NSString alloc] initWithFormat:@"%@%@",_datTeacher.department, @" Department"];
        _wayToReach.text = _datTeacher.toReach;
        _funFact.text = _datTeacher.funFact;
        _room.text = _datTeacher.room;
        _teacherImage.image = [UIImage imageNamed: [[NSString alloc] initWithFormat:@"%@%@",_datTeacher.tname, @".TIF"]];
        _classList = _datTeacher.cList;
    
    }
    - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
    {
    	return YES;
    }
    
    -(void) segueMethod
    { 
        [self performSegueWithIdentifier:@"toSchedule" sender: self];
    }
    
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
        return @"Classes:";
    }
    @end

    Here is code for the tableviewcellclass

    Code:
    @interface ClassViewCell : UITableViewCell
    @property (strong, nonatomic) IBOutlet UILabel *Period;
    @property (strong, nonatomic) IBOutlet UILabel *cName;
    - (IBAction)buttonIsPressed:(id)sender;
    //Want segue to take place^^
    @end
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    You haven't really told us what the problem is, though. Are you getting warnings/errors/crashes? Something else? What's not working?

    Also, what resources have you been using to become familiar with the fundamentals of Objective-C / iOS Programming. Please be specific.
     
  3. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #3
    Any time you invoke a segue, the system looks for a prepareForSegue:sender: method in the view controller that is triggering the segue.

    If you implement that method, it gets called.

    That method gets passed a pointer to the segue object. The segue object has properties for both the source and destination view controller. After invoking a segue, you can put code in your prepareForSegue:sender: method that gets the destination view controller and saves a pointer to it.

    You can also set up the destination view controller to have a delegate property, and the invoking VC can set itself as the delegate of the invoked VC. This is documented in Apple's docs on segues.
     
  4. LT21j thread starter macrumors newbie

    Joined:
    Apr 8, 2013
    #4
    I am have been using various sources. Mostly the apple development API and a book called ios essentials. The reason I'm having trouble is I cannot say [self preformSegueWithIdentifier: @"toSchedule" sender: self] becuase this line takes place in the code of the class tableCellView in the method -(IBAction) buttonPressed: sender (id). So I need to replace the self with and instance of the containing class view controller.
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Can you be more specific? Normally when referencing a book, it's courteous to provide exact title, author, and edition (and chapter and page number, if applicable).
     
  6. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6
    You need to post an explanation of how your code is structured, and post the code. Are you using storyboards or XIB files? How are you managing your table view? Which object is it's delegate and data source? How are cells created?

    It sounds like you have a custom table view cell class, and controls in the cells are triggering actions in the cell object instead of in the view controller.

    Is that right?
     
  7. LT21j thread starter macrumors newbie

    Joined:
    Apr 8, 2013
    #7
    The book is called IPhone IOS 6 Development Essentials by Neil Smyth. But the project I am trying to make is not discussed in the book.
     
  8. LT21j thread starter macrumors newbie

    Joined:
    Apr 8, 2013
    #8
    I posted some of the code above. Is that enough?
     
  9. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #9
    Ok, I'm right that you have a custom cell class.

    Here's a way to handle it.

    Give your custom cell class a delegate property.

    Define a protocol that your cell uses to notify it's delegate (the view controller) about button taps. Create a method

    Code:
    - (void) cell:(UITableViewCell*) cell
      buttonTappedWithIdentifier: (NSString *) identifier;
    Then have the buttons in your table view invoke action methods that send a notification to the delegate about which cell was tapped, and a code the delegate can use to figure out what action should be performed.

    In the view controller implementation of that method, you can query the table view for the index path of the cell (using the table view method indexPathForCell). Then you can use the identifier code to figure out what to do with the button tap.

    In your case, you could have your cell button action send a segue identifier to the delegate (the view controller) and invoke that segue. Then in prepareForSegue you could pass in the indexPath of the cell that was tapped.
     
  10. MattInOz macrumors 68030

    MattInOz

    Joined:
    Jan 19, 2006
    Location:
    Sydney
    #10
    In that case how does tapping the button differ from tapping the cell and reacting to that cell selection?

    Couldn't you use the tableview delegate methods:-
    – tableView:didSelectRowAtIndexPath:
    or if you want two different behaviours
    – tableView:accessoryButtonTappedForRowWithIndexPath:

    Presenting your new view from one of those.

    Your TableviewController (which is normally the delegate) will have a link to the navigation controller it lives within, making it easy to add a new view to the navigation stack. Also avoids the cells from having links back up the tree.
     
  11. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #11
    Thanks for the clarification.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    LT21j, I see you edited your original post to include the code. I would like to ask you two things:
    1. Please post the code as a followup rather than editing the original post, to help preserve the conversation flow of the thread
    2. Please use
      Code:
       tags when posting code snippets to prevent formatting side-effects. I have taken the liberty of editing your original post to use them.
      [/LIST]
     
  13. LT21j thread starter macrumors newbie

    Joined:
    Apr 8, 2013
    #13
     

Share This Page