简体   繁体   English

从另一个类为超级视图创建子视图不起作用

[英]Creating subview for superview from another class is not working

Hi i am new to the iOS. 嗨,我是iOS的新手。

I have 2 classes one is parent class and another one is child class. 我有2个班级,一个是父母班,另一个是孩子班。 i am having one method in parent class that will create a UIScrollView. 我在父类中有一个方法将创建一个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. 从子类调用时将调用该方法,但如果我通过使用self在父类中调用相同的方法,则不会创建UIScrollView,而是创建UIScrollView。

I do not know where i am making problem. 我不知道我在哪里制造问题。 Please guide me 请指导我

//scrollview creation method in parent class// //父类中的scrollview创建方法//

  -(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];

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. ü正在创建的另一个目的ParentViewController并调用creatingScroll该对象,这是不是被推到您的viewController视图的方法。

U can call the parent class method by using protocols & delegates. U可以使用协议和委托来调用父类方法。

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

hope it helps. 希望能帮助到你。 happy coding :) 快乐的编码:)

由于您已经继承了subclass superclass中所有superclass的方法,因此您只需要在subclass superclass中调用[self creatingScroll]

  -(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];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM