Moumou
Moumou

Reputation: 1532

iOS Custom View's methods not called

I have a view controller CaptureDataVC and a custom view KeyboardView.

KeyboardView has all the init functions required to work as needed:
-(id)initWithCoder:(NSCoder *)aDecoder and -(id)initWithFrame:(CGRect)frame

In CaptureDataVC's xib file I have a UIView with custom class KeyboardView.

The view renders just fine. The problem is that I cannot call any of the methods in KeyboardView (It compiles and I don't get any errors but the methods aren't called).

So basically I can't do this:

CaptureDataVC.h

@property (nonatomic,retain) IBOutlet KeyboardView  *keyboardView;

CaptureDataVC.m

[self.keyboardView foo];

KeyboardView.m

-(void) foo {
    NSLog(@"Hello World!"); //Won't print anyting
}

Any ideas ?

Edit:

enter image description here

The custom class is set to the blue view

Upvotes: 6

Views: 569

Answers (5)

Faisal Tanveer
Faisal Tanveer

Reputation: 66

There is no need to initialize custom view manually as its added in view controller. All you have to do just check where you have been calling your custom view method. It should be called after your view loaded. i.e viewDidLoad or viewWillAppear method.

Upvotes: 2

user4169870
user4169870

Reputation:

you can achieve your desired result using the delegates.implement the delegates of keyboard view in your viewcontroller.

Upvotes: 1

MUHAMMAD WASIM
MUHAMMAD WASIM

Reputation: 191

In CaptureDataVC.m you must allocate the memory to object as:

self.keyboardView=[[KeyboardView alloc]init];

and the call your method as:

[self.keyboardView foo];

it will work.

Upvotes: 1

NixSolutionsMobile
NixSolutionsMobile

Reputation: 191

Please check if KeyboardView`s outlet property setup: if property is nil, nothing will happen

Upvotes: 5

Gwendal Roué
Gwendal Roué

Reputation: 4054

Well, the first questions are:

  1. Is the [self.keyboardView foo]; line run? Put a breakpoint there.
  2. If yes, is self.keyboardView not nil? When the debugger has stopped on the previous breakpoint, type po self.keyboardView in the debugger to check.

A classic mistake is to assume that self.someView is always set. Actually it won't, before view and outlets have been loaded. See the documentations of viewDidLoad, and isViewLoaded.

Upvotes: 7

Related Questions