2014-07-23 71 views
0

我怎樣才能重新在斯威夫特消氣從目標C斯威夫特

- (UIPageControl *)pageControl { 
    if (!_pageControl) { 
     _pageControl = [[UIPageControl alloc] init]; 
     _pageControl.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin; 
     _pageControl.frame = (CGRect){0, 35, CGRectGetWidth(self.bounds), 0}; 
     _pageControl.hidesForSinglePage = YES; 
     _pageControl.currentPage = self.currentPage; 
     _pageControl.currentPageIndicatorTintColor = [UIColor whiteColor]; 
     _pageControl.pageIndicatorTintColor = [UIColor colorWithWhite:1.0 alpha:.5]; 
    } 
return _pageControl; 
} 

此獲取代碼這是我目前有

var pageControl: UIPageControl { 
get{ 
    if (self.pageControl == nil) { 
     var pageControl: UIPageControl= UIPageControl.alloc() 
     pageControl.autoresizingMask = .FlexibleWidth | .FlexibleTopMargin; 
     pageControl.frame = CGRectMake(0, 35, CGRectGetWidth(self.bounds), 0) 
     pageControl.hidesForSinglePage = true; 
     pageControl.currentPage = self.currentPage; 
     pageControl.currentPageIndicatorTintColor = UIColor.whiteColor() 
     pageControl.pageIndicatorTintColor = UIColor(white: 1.0, alpha: 0.5) 
     return pageControl 
    } 
    return self.pageControl 
} 
} 

但是,這是不一樣的,因爲它不是設置self.pageControl如果我嘗試添加此

self.pageControl = pageControl 

我得到這個錯誤「無法分配到‘自我’的PageControl'」

+0

and where is your _setter_? – holex

回答

1

你的酒店有一個getter,所以是一個「計算性能」,結果它沒有制定者(除非你提供一個你自己)。我懷疑你想要做的就是回到你的計算性能與私人存儲的屬性:

private var _pageControl: UIPageControl? 

var pageControl: UIPageControl { 
get{ 
    // if the private backing variable doesn't exist, create it 
    if (_pageControl == nil) { 
     _pageControl = UIPageControl() 
     // ... 
    } 
    return _pageControl 
} 
} 

或者,看看Lazy Stored Properties

0

耶!這是在快速getter的問題,你需要創建單獨的方法來獲取pageControl。

func getPageControl() { 

    if (self.pageControl == nil) { 
     self.pageControl = UIPageControl.alloc() 
     self.pageControl.autoresizingMask = .FlexibleWidth | .FlexibleTopMargin; 
     self.pageControl.frame = CGRectMake(0, 35, CGRectGetWidth(self.bounds), 0) 
     self.pageControl.hidesForSinglePage = true; 
     self.pageControl.currentPage = self.currentPage; 
     self.pageControl.currentPageIndicatorTintColor = UIColor.whiteColor() 
     self.pageControl.pageIndicatorTintColor = UIColor(white: 1.0, alpha: 0.5) 

    } 
    return self.pageControl 
} 

希望這有助於

0

在這樣的情況下,你只有一次創建控件,你也可以使用lazy存儲的財產,例如:

class MyClass { 
    lazy var pageControl = MyClass.makePageControl() 

    private class func makePageControl() -> UIPageControl { 
     let pg = UIPageControl() 
     pg.autoresizingMask = .FlexibleWidth | .FlexibleTopMargin; 
     // … 
     return pg 
    } 
}