开发者

Calling a method in the ViewController from a subClass

开发者 https://www.devze.com 2023-03-03 13:23 出处:网络
I was wondering how I call a method in my ViewController from a subclass? I tried to call this from the viewDidLoad of my subClass.m file, but xCode tells me that request for member \'viewController\

I was wondering how I call a method in my ViewController from a subclass?

I tried to call this from the viewDidLoad of my subClass.m file, but xCode tells me that request for member 'viewController' in something not a structure or union:

[self.viewController tabAdd:@"Extra" inColour:@"Green" withReference:0];
[self.viewController resetTabsView];

In my viewController I defined the methods as follows:

-(void)tabAdd:(NSString *)newTabTitle inColour:(NSString *)newTabColour withReference:(NSUInteger)newTabReference;
-(void)resetTabsView;

Thanks for your help!


This is how I set up the VC and subclass...

(1) TabsEdit.H:

#import <UIKit/UIKit.h>
#import "MyViewController.h"

@interface TabsEdit : MyViewController <UITextFieldDelegate> {

    IBOutlet UITextField *enterTitle;

}

@property (nonatomic, retain) UITextField *enterTitle;
@end

(2) MyViewController.H:

    //

#import <UIKit/UIKit.h>
#import "TabsEdit.h"

@class TabsEdit;

@interface MyViewController : UIViewController <UITextViewDelegate, UITextFieldDelegate> {

// ...

//@property (nonatomic, retain) TabsEdit *tabsEdit;

in M. File:

@synthesize tabsEdit;

And the I init it like this:

TabsEdit *tEdit = [[TabsEdit alloc] i开发者_开发技巧nitWithNibName:@"TabsEdit" bundle:nil];
self.tabsEdit = tEdit;
[self.view addSubview:tEdit.view];
[tEdit release];


[self tabAdd:. .....]

you are derived from VC, so call method directly.

I think you can do a [super tabAdd: .....] as well.


As per comments above, you need your sub class to say what it is a sub class of. The first line of the interface should be:

@interface TabsEdit : MyViewController <UITextFieldDelegate> {

Then as @DavidNeiss said use [self tabAdd: ...] in the subclass. A class can call all of its own methods and all of the methods belonging to its parent classes using "self".

Use "super" in cases where you have overridden a parent class's method with one of your own and you need to call the parent's method and not your own. For example when you have a custom -(id)init for your subclass you should call[super init] in it somewhere to take care of any setup that the parent class does in its -(id)init.


If you want to call a method in a super class from a subClass just go:

[super methodName];

I don't see why you can just go [self methodName]; either. The subclass should have access to all superclass methods.

0

精彩评论

暂无评论...
验证码 换一张
取 消