0

In View Controller.m

@interface ViewController ()
{
    CustomView *view;
}

@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];

    view = nil;
    view = [[CustomView alloc]init];

    [self.view addSubview:view];
}

In CustomView.m

-(CustomView *)init
{
    CustomView *result = nil;
    result = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil] objectAtIndex:0];

    return result;
}

I have two buttons in my custom view. My Custom view was loaded fine as expected but button actions not fired if enable ARC for CustomView.m file, If I disable ARC then Button actions are firing…

Where Im going wrong..

Is it the correct way of loading a nib of uiview (which i want to use in many places in my project..)

Thank you ..

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
siva krishna
  • 1,149
  • 2
  • 15
  • 23

1 Answers1

0

This is a very confusing/confused implementation of an init method.

- (CustomView *)init
{
    CustomView *result = nil;
    result = [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil] objectAtIndex:0];

    return result;
}

I'd suggest changing it to something like this...

// class method not instance method
+ (CustomView *)loadFromNib {
    return [[[NSBundle mainBundle] loadNibNamed:@"CustomView" owner:self options:nil] objectAtIndex:0];
}

Then changing your ViewController method to something like this...

@interface ViewController ()

@property (nonatomic, strong) CustomView *customView; // don't call it view, it's confusing

@end

@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];

    self.customView = [CustomView loadFromNib];

    [self.view addSubview:self.customView];
}

The problems you are having are possibly coming from the way you implemented the init method as an instance method but then ignored the instance and returned a new instance.

The memory implications of that are confusing and hard to work out.

Fogmeister
  • 76,236
  • 42
  • 207
  • 306
  • Im getting exception while loading nib, this class is not key value coding-compliant for the key "btnLoc" Here btnLoc is one of my button name – siva krishna Jan 21 '16 at 11:21
  • @sivakrishna From where? Which class is giving this? Where are you trying to set btnLoc? Sounds like you possibly haven't set the subclass of the view in the nib to `CustomView`. Hard to say without seeing any code from that class. – Fogmeister Jan 21 '16 at 11:23
  • I set the file Owner of custom view to "CustomView" not for the Custom View – siva krishna Jan 21 '16 at 11:29
  • @sivakrishna yeah, don't do that. Just select the view from the nib and set the subclass of the view (not the files owner). You need to attach the outlets in the view to the view not the files owner. – Fogmeister Jan 21 '16 at 11:30
  • Could U please clarify me whats the difference between them ? – siva krishna Jan 21 '16 at 11:34
  • @sivakrishna https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/LoadingResources/CocoaNibs/CocoaNibs.html Just ignore the files owner. In this particular case you are using the nib to load a view. You don't need the files owner. You need to set the view to be the right kind of view. – Fogmeister Jan 21 '16 at 11:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101289/discussion-between-fogmeister-and-siva-krishna). – Fogmeister Jan 21 '16 at 11:48
  • If i set to the view, I want to few more clarifications I have two list arrays which i want to show on clicking button some think like in pickers.. Where(in which class) I want to run my service to get the data ? – siva krishna Jan 21 '16 at 11:48