thavasidurai
thavasidurai

Reputation: 2002

Creating subview for superview from another class is not working

Hi i am new to the iOS.

I have 2 classes one is parent class and another one is child class. i am having one method in parent class that will create a UIScrollView. and i am trying to call that parent class method from child class by creating object of parent class. that method is called when i am calling from child class but it does not create a UIScrollView if i call same method in parent class by using self it creates UIScrollView.

I do not know where i am making problem. Please guide me

//scrollview creation method in parent class//

  -(void)creatingScroll
{
UIScrollView * propertyScrl = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 200, 320, 160)];
propertyScrl.scrollEnabled = YES;
propertyScrl.showsHorizontalScrollIndicator = YES;
propertyScrl.contentSize = CGSizeMake(600, 60);
propertyScrl.backgroundColor = [UIColor redColor];
[self.view addSubview:propertyScrl];

}

//calling above method from child class//

    ParentViewController *vc = [[ParentViewController alloc]init];

    [vc creatingScroll];

Upvotes: 1

Views: 234

Answers (3)

AppleDelegate
AppleDelegate

Reputation: 4249

  -(UIScrollView *)creatingScroll
{
UIScrollView * propertyScrl = [[UIScrollView alloc]initWithFrame:CGRectMake(10, 200, 320, 160)];
propertyScrl.scrollEnabled = YES;
propertyScrl.showsHorizontalScrollIndicator = YES;
propertyScrl.contentSize = CGSizeMake(600, 60);
propertyScrl.backgroundColor = [UIColor redColor];
 //[self.view addSubview:propertyScrl];  // don't add here

return propertyScrl;

}

//calling above method from child class or any class//
probably in viewDidLoad of child class 

 ParentViewController * vc = [[ParentViewController alloc]init];

  UIScrollView * scrollView = (UIScrollVIew * ) [vc creatingScroll];

    [self.view addSubView:scrollView];

Upvotes: 0

MrWaqasAhmed
MrWaqasAhmed

Reputation: 1489

Since you already have inherited all the methods of superclass in your subclass, then you just need to call [self creatingScroll] in the subclass.

Upvotes: 0

Anshuk Garg
Anshuk Garg

Reputation: 1540

u are creating another object of ParentViewController and calling creatingScroll method on that object, which is not the view that is pushed onto your viewController.

U can call the parent class method by using protocols & delegates.

please refer http://mobiledevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

hope it helps. happy coding :)

Upvotes: 1

Related Questions