Flying
Flying

Reputation: 169

How to call a method of uiview class in mainViewController in objective c

I make a method setupLabels in a custom uiview class and i, m trying to call this method in mainViewController viewDidLoad method using [self setupLabels] but i gives error of undefined setupLabels, How can i access -(void)setUpLabels{ method from a uiview class into mainviewController?

Upvotes: 1

Views: 1482

Answers (1)

Rahul
Rahul

Reputation: 5834

You are getting error because your setUpLabels method is in your custom class and ViewController doesn't have the reference to it. You need to provide reference to use method on other classes.

Firstly you need to make an object of your custom UIView class. Put your method -(void)setUpLabels in .h file of your custom view class. Implement it in .m of your custom class.

Now in viewDidLoad of your view controller based on the type of your Custom Class you have to create Object of that view:

If you are using xib:

   MyCustomView *customView = [[[NSBundle mainBundle] loadNibNamed:@"MyCustomView" owner:nil options:nil] firstObject];
   [customView  setUpLabels];

If not using Xib

   MyCustomView *customView =  [[MyCustomView alloc] init];
   [customView  setUpLabels];

At last in your viewDidLoad method:

  - (void)viewDidLoad
   {
        MyCustomView *customView =  [[MyCustomView alloc] init];
        customView.frame = CGRectMake(100,10,200,100);
        [customView  setUpLabels];
        [self.view addSubview:customView];
   }

Upvotes: 3

Related Questions