簡體   English   中英

從另一個類為超級視圖創建子視圖不起作用

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

嗨,我是iOS的新手。

我有2個班級,一個是父母班,另一個是孩子班。 我在父類中有一個方法將創建一個UIScrollView。 我試圖通過創建父類的對象從子類中調用該父類方法。 從子類調用時將調用該方法,但如果我通過使用self在父類中調用相同的方法,則不會創建UIScrollView,而是創建UIScrollView。

我不知道我在哪里制造問題。 請指導我

//父類中的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];

}

//從子類中調用上述方法//

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

    [vc creatingScroll];

ü正在創建的另一個目的ParentViewController並調用creatingScroll該對象,這是不是被推到您的viewController視圖的方法。

U可以使用協議和委托來調用父類方法。

請參閱http://mobiledevelopertips.com/objective-c/the-basics-of-protocols-and-delegates.html

希望能幫助到你。 快樂的編碼:)

由於您已經繼承了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