Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
882 views
in Technique[技术] by (71.8m points)

iphone - How to call a method of another Class?

EDIT2: I try to summarize my problem and the solutions:

I've got a TableViewController named DetailedViewController. My intention was to activate TouchesBegan to recognize actions like sliding etc, and normally, the method touchesbegan is replaced with the DidSelectRow method. In many posts on stackoverflow, subclassing the UITableView is the only possibility to realize this.

So i created a SpecificTable with .xib file and i used this as a subclass of UITableViewController by adding the SpecificTable as the nib-file. Selecting a row works fine, and also the TouchesBegan method (i called a method IN the SpecificTable.m with an Alert.) But now i want to call a Method in the UITableViewController (DetailedViewController) where moveToNextItem is declared like

 -(void)moveToNextItem:(id)sender
{
 [self.navigationController
 pushViewController:bbarChart animated:YES];
}

But by calling this method with [self moveToNextItem] the App crashes by touching. (in the Debugger-Mode, the App crashes in the line of [self moveToNextItem]. What is the right way to call the method of DetailedViewController.m?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Update: You should probably subclass UITableViewCell rather than UITableView. Then in your table view controller's cellForRowAtIndexPath: method, return an instance of this subclass rather than an instance of UITableViewCell.

You will also need to pass a DetailedViewController pointer on to the cell, so that you can invoke its moveToNextItem method in the touchesBegan, etc. methods.

Adapt this example to your needs:

MyTableViewCell.h

@class DetailedViewController;
@interface MyTableViewCell : UITableViewCell {
    DetailedViewController *dvc;
}
@property (nonatomic, retain) DetailedViewController *dvc;
@end

MyTableViewCell.m

#import "MyTableViewCell.h"
#import "DetailedViewController.h"
@implementation MyTableViewCell
    @synthesize dvc;
    - (void)someMethod { // This would be your touchesBegan, etc. methods
        [dvc moveToNextItem];
    }
    - (void)dealloc {
        [dvc release]; // We retained dvc so we have to release it when we're done with it
        [super dealloc];
    }
@end

DetailedViewController.h

@interface DetailedViewController : UITableViewController {
    // iVars here
}
// Methods and properties here
- (void)moveToNextItem;
@end

DetailedViewController.m

#import "DetailedViewController.h"
#import "MyTableViewCell.h"
@implementation DetailedViewController
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyTableViewCell"];
        if(cell == nil) {
            cell = [[[MyTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MyTableViewCell"] autorelease];
            cell.dvc = self; // This gives the cell a reference to the detailed view controller
        }
        return cell;
    }
    - (void)moveToNextItem {
        // ...
    }
@end

There are probably far better ways to achieve what you want, but this is the best I can do without more information.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...