Calling method with parameter?

Discussion in 'iOS Programming' started by Danneman101, Oct 26, 2009.

  1. macrumors 6502

    Joined:
    Aug 14, 2008
    #1
    Ive got a function with the following syntax:

    Code:
    - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
    { // do stuff }
    
    It gets triggered automatically by pressing a row in a UITableView.

    On some occasions I want to trigger this function from another action, like pressing a button or something. But I cant figure out the code to call it.

    Lets say I want to call the function as if I had pressed the second row in the tableview. My limited knowledge of obj-c tells me I should do this:

    Code:
    [MyViewController didSelectRowAtIndexPath: 1];
    
    ..where "MyViewController" is the viewcontroller that the tableview is located in, and the button from which Im calling this function is from AppDelegate.m. I do have a reference to that viewcontroller from there, since Ive imported it using:

    Code:
    #import "MyViewController.h"
    
    Instead of working I get a warning saying
    "warning: 'MyViewController' may not respond to '+didSelectRowAtIndexPath'
    (Messages without matching method signature will be assumed to return 'id' and accept '...' as arguments"

    Any ideas? Thansk/ Daniel
     
  2. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #2
    Each ":" in the method takes a parameter. You are missing the first one: tableView.
     
  3. thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #3
    So the syntax would be something like this instead?

    Code:
    UITableView *myTableView;
    [MyViewController tableView:myTableView didSelectRowAtIndexPath:1];
    
    This results in the same type of warning as before, and hangs my app. So Im still off base.

    Could it have to do with me trying to pass 1 as a parameter instead of a NSIndexPath? I was under the assumption that an NSIndexPath related to a number, but perhaps Im wrong. Gonna check the docs, but is the obj-c function call syntactically correct?
     
  4. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #4
    Yes. As NSIndexPath is not typedefed to int. In fact it's an object.
     
  5. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    Also, be sure that MyViewController is the name of an instance of the UITableViewController and not the name of a class. Otherwise you are accessing the method as a class-method (defined with the + ) and not an instance-method (defined with the - ). That's why it is a common convention to name classes in camel-case starting with an upper-case and instances starting with a lower-case letter, as in:
    Code:
    MyViewController *myViewController = ...
     
  6. thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #6
    Actually Ive been trying both methods, calling using the class-name and the instance-name.

    Using the instance-method (which should then be the correct way) Im getting the error "'myViewController' undeclared (first use in this function)".

    This despite my efforts to create an instance of MyViewController by doing the following:

    AppDelegate.h
    Code:
    @interface AppDelegate : NSObject <UIApplicationDelegate, AVAudioPlayerDelegate, UIAccelerometerDelegate>
    {
    	IBOutlet MyViewController	*myViewController;	
    }
    @property (nonatomic, retain) Level1ViewController *level1ViewController;
    
    AppDelegate.m
    Code:
    #import "MyViewController.h"
    
    @synthesize myViewController;
    // gives error: no declaration of property 'myViewController' found in the interface
    
    I thought this was how you created an instance of another class so you could call its functions?
     
  7. thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #7
    Ok, I finally found another way of reaching a function in another class. This example excludes the parameters, but its still equally strange to someone not as familiar with the obj-c syntax:

    Code:
    [((MyViewController*)navigationController.topViewController) MyFunction];
    
    If you have an answer to why the code in the previous post was not successful in creatin an instance, I would very much like to hear it to get an increased understanding of the lingo :)
     
  8. Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    You've created a property for level1ViewController and not myViewController.

    And besides, you've defined an instance variable for myViewController but you haven't actually instantiated it yet, unless that's in some code you haven't included. You would do that via:
    Code:
    self.myViewController = [[MyViewController alloc] init];
     
  9. thread starter macrumors 6502

    Joined:
    Aug 14, 2008
    #9
    The first objection was just a missprint from converting my code to an easily readable sample - sorry about the confusion.

    The second issue was, however, spot on. Things are running fine now, and Im moving on to other problems :p

    Thanks so much for your help :)
     

Share This Page