Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 960
Moumou Avatar asked Sep 15 '15 13:09

Moumou


4 Answers

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.

like image 77
Gwendal Roué Avatar answered Nov 03 '22 22:11

Gwendal Roué


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

like image 25
NixSolutionsMobile Avatar answered Nov 03 '22 22:11

NixSolutionsMobile


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.

like image 2
Faisal Tanveer Avatar answered Nov 03 '22 23:11

Faisal Tanveer


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.

like image 1
MUHAMMAD WASIM Avatar answered Nov 03 '22 21:11

MUHAMMAD WASIM