簡體   English   中英

如何在iOS8 Objective C中使用圖像設置UIPageControl?

[英]How to set UIPageControl with image in iOS8 Objective c?

我想用圖像代替點設置UIPageControl 我實現了以下代碼,但始終會崩潰。請幫助我。

-(void)updateDots
{
    for (int i=0; i<[_pageControl.subviews count]; i++) {

    UIImageView *dot = [_pageControl.subviews objectAtIndex:i];
    if (i==_pageControl.currentPage)
        dot.image = [UIImage imageNamed:@"on.png"];
    else
        dot.image = [UIImage imageNamed:@"off.png"];

   }
}

在iOS8中,由於未捕獲的異常'NSInvalidArgumentException',我收到以下錯誤***終止應用程序,原因:'-[UIView setImage:]:無法識別的選擇器發送到實例0x7f9dd9eaabb0'

您不應該假定UIPageControl將包含UIImageViews (不包含)。

使用公共API的方法如下:

- (void)updateDots
{
    // this only needs to be done one time
    // 7x7 image (@1x)
    _pageControl.currentPageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"on.png"]];
    _pageControl.pageIndicatorTintColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"off.png"]];
}

您正在for循環中獲取UIView。 檢查點是否為UIImageView Class然后設置圖像。

id object = [_pageControl.subviews objectAtIndex:i];
if([object isKindOfClass:[UIImageView Class]])
{
    UIImageView *dot = (UIImageView *)object;
    if (i==_pageControl.currentPage)
        dot.image = [UIImage imageNamed:@"on.png"];
    else
        dot.image = [UIImage imageNamed:@"off.png"];
}

希望對您有幫助。

請記住,子視圖不一定總是必須為UIImageView 例如, UITableViewCell經常在其中包含封閉視圖。 您需要在下面進行更改。

-(void)updateDots
{
    for (int i=0; i<[_pageControl.subviews count]; i++) {

    if ([[_pageControl.subviews objectAtIndex:i] isKindOfClass:[UIImageView Class]]) 
    {
        UIImageView *dot = (UIImageView *)[_pageControl.subviews objectAtIndex:i];
        if (i==_pageControl.currentPage)
            dot.image = [UIImage imageNamed:@"on.png"];
        else
            dot.image = [UIImage imageNamed:@"off.png"];
        }
    }        
}

我注意到,在UITableViewCell ,單元格內容始終包含一個封閉的UIView 您需要遞歸地進入該視圖以找到所需的UIImageView

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM