views:

152

answers:

4

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?

+1  A: 

Declare the method in DetailedViewController.h, and @import that file in SpecificTable.h.

Frederik Slijkerman
sry, but this doesn't work. I tried this even before i asked.
Jonathan
I think you mean `#import` rather than `@import`...
robinjam
+4  A: 

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.

robinjam
+1 precise :), would not it be good to explanation of **WHY** as well?
Gollum
sry, this doesn't work, i also thought that this would works.
Jonathan
This does work, and it how you do what you say you are trying to do. Post your code, because either you have a strange error somewhere, or you are trying to something other than what you describe.
Squeegy
Ok, i try to write all connections... In the IB, i've got a UITableViewController named DetailedViewController, an the NIB File is the SpecificTable, a new class containing ".h, .m, .xib"In This Class, i overwrite the TouchesBegan, TouchesMoved etc methods including this in the TouchesMoved method: if (mystartTouchPosition.x < currentTouchPosition.x) { isProcessingListMove = YES; [self moveToNextItem]; return; }In The Implementation File of the UITableViewController (DetailedViewController) i added the method like i posted in my Edit-Party of my question.
Jonathan
Did you get a compiler error or a runtime error?
robinjam
oh, sry, i always forgot to write the error, sry.First, there is a warning named "'SpecificTable' may not respond to '-moveToNextItem'" and if i call the method (by touching) the App crashes..
Jonathan
Look in "SpecificTable.h" and make sure it actually does inherit from `DetailedViewController`. See the edited version of my answer above for a description of how to do this.
robinjam
Oh, i think,i made a mistake, sry. The DetailedViewController is the TableViewController. And now i want to subclass the TableView. So i use the SpecificTable like:#import <UIKit/UIKit.h>#import <QuartzCore/QuartzCore.h>@class DetailedViewController;@interface SpecificTable : UITableView{ DetailedViewController * detailedViewController;}- (void)moveToNextItem:(id)sender;@property (nonatomic, retain) IBOutlet DetailedViewController *detailedViewController;@endand the TouchesBegan method works, because i also added a moveToNextItem method in the SpecificTable and it works.
Jonathan
Oh! Well that changes everything...
robinjam
I've no idea why you would want to subclass a UITableView. Could you edit your question to explain exactly what you're trying to do?
robinjam
ok, i edited my question and i hope you understand what i intend to do. Thank you for your willingness to help me.
Jonathan
Thank you very much, your tips where very helpful and now all works! Thanks
Jonathan
You're welcome! I'm glad you got it working in the end.
robinjam
A: 

Is [NSNotificationCenter defaultCenter] something you are looking for?

ohho
I'm not really sure how this relates to the question. Care to elaborate?
robinjam
To communicate between two different view controller, sometimes there is no convenient way to access to another view controller's instance / methods. One way to communicate is to broadcast a notification, other interested view controller can subscribe/listen to such notifications. I don't understand why my answer is down-voted. Down-voter please explain.
ohho
A: 

if SpecificTable is really a subclass of DetailedViewController you can call

[self moveToNextItem];

as already mentioned.

but i think you mean a subview or not? so SpecificTable.view is a subview ob DetailedViewController.view

you have several options then. for example using NSNotificationCenter.

or what is probably also a good way for you is to setup an instance variable of DetailedViewController in your SpecificTable and assign it when you init your SpecificTable.

as an example:

// the parent view .m
testTVC *tableview = [[testTVC alloc] initsomething];
tableview.parentVC = self;
[self.view addSubView:tableview.view];
[tableview release];

now in your testTVC

// the .h
@interface testTVC : UITableViewController {
    testVC *parentVC;
}

@property(nonatomic,retain) testVC *parentVC;

@end


// the .m
[parentVC moveToNextItem];

you also have to synthesize and release your parentVC.

choise
Thank you a lot, but i'm very sure that i mean subclass. So i've got an UITableViewController named DetailedViewController and there i declared the tableView. In The IB, the NIB is the subclass, SpecificTable (type:UITableView) and there the tableView is declared. (I used this to enable TouchesBegan and so on in the TableViewCell).
Jonathan
so you have to provide us more code and/or the way your stuff is conneted in IB. normally `[self moveToNextItem];` should work.
choise
i posted the connection in the IB under the answer of robinjam, i hope that can help, otherwise ask, thank you.
Jonathan