Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
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
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
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?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Code:
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:
Code:
MyViewController *myViewController = ...
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
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:
Code:
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
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?
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
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 :)
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
Code:
...
{
	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:
Code:
self.myViewController = [[MyViewController alloc] init];
 

Danneman101

macrumors 6502
Original poster
Aug 14, 2008
361
1
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];

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 :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.