PDA

View Full Version : Calling method with parameter?




Danneman101
Oct 26, 2009, 02:59 PM
Ive got a function with the following syntax:


- (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:


[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:


#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



dejo
Oct 26, 2009, 03:06 PM
Each ":" in the method takes a parameter. You are missing the first one: tableView.

Danneman101
Oct 26, 2009, 04:17 PM
So the syntax would be something like this instead?


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?

robbieduncan
Oct 26, 2009, 04:24 PM
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?

Yes. As NSIndexPath is not typedefed to int. In fact it's an object.

dejo
Oct 26, 2009, 04:34 PM
UITableView *myTableView;
[MyViewController tableView:myTableView didSelectRowAtIndexPath:1];

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:
MyViewController *myViewController = ...

Danneman101
Oct 26, 2009, 06:27 PM
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:
MyViewController *myViewController = ...

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

@interface AppDelegate : NSObject <UIApplicationDelegate, AVAudioPlayerDelegate, UIAccelerometerDelegate>
{
IBOutlet MyViewController *myViewController;
}
@property (nonatomic, retain) Level1ViewController *level1ViewController;


AppDelegate.m

#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?

Danneman101
Oct 26, 2009, 06:55 PM
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:


[((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 :)

dejo
Oct 27, 2009, 01:04 AM
...
{
IBOutlet MyViewController *myViewController;
}
@property (nonatomic, retain) Level1ViewController *level1ViewController;

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:
self.myViewController = [[MyViewController alloc] init];

Danneman101
Oct 27, 2009, 06:31 AM
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:
self.myViewController = [[MyViewController alloc] init];

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 :)