如何访问pageControl viewController的特定视图?

时间:2013-01-21 13:52:12

标签: iphone ios xcode

您好我正在创建一个项目,其中从服务器加载多个图像有一些像计数和注释计数和一个按钮喜欢图像。我使用像PageControl这样的滑块控制器来显示各个图像。

这是我显示视图的代码

-(UIView*)reloadView:(DPSliderView *)sliderView viewAtIndex:(NSUInteger)idx
{
    _loading_view.hidden=TRUE;
    if (idx < [photos count]) {
        NSDictionary *item = [photos objectAtIndex:idx];

        PhotoView *v = [[PhotoView alloc] init];
        v.photoIndex = idx;
        v.imageView.imageURL = [DPAPI urlForPhoto:item[@"photo_220x220"]];
        NSString *placename1 = [item valueForKeyPath:@"spotting.item.name"];
        NSString *firstCapChar1 = [[placename1 substringToIndex:1] capitalizedString];
        NSString *cappedString1 = [placename1 stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar1];
        v.spotNameLabel.text = cappedString1;

        NSString *placename = [item valueForKeyPath:@"spotting.place.name"];
        NSString *firstCapChar = [[placename substringToIndex:1] capitalizedString];
        NSString *cappedString = [placename stringByReplacingCharactersInRange:NSMakeRange(0,1) withString:firstCapChar];
        NSString *place1=[NSString stringWithFormat:@"%@",cappedString];
        NSString *address=[NSString stringWithFormat:@"%@",[item valueForKeyPath:@"spotting.place.address"]];

        NSString *location_str3 = [NSString stringWithFormat:@"@ %@, %@",place1,address];
        int cap_len=[place1 length];
        int address_lenth=[address length];

        ZMutableAttributedString *str = [[ZMutableAttributedString alloc] initWithString:location_str3
                                                                              attributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                                          [[FontManager sharedManager] zFontWithName:@"Lucida Grande" pointSize:12],
                                                                                          ZFontAttributeName,
                                                                                          nil]];
        [str addAttribute:ZFontAttributeName value:[[FontManager sharedManager] zFontWithName:@"Lucida Grande" pointSize:12] range:NSMakeRange(0, cap_len+3)];

        [str addAttribute:ZForegroundColorAttributeName value:[UIColor colorWithRed:241/255.0f green:73.0/255.0f blue:2.0/255.0f alpha:1.0]range:NSMakeRange(0, cap_len+3)];

        [str addAttribute:ZForegroundColorAttributeName value:[UIColor colorWithRed:128.0/255.0f green:121.0/255.0f blue:98.0/255.0f alpha:1.0]range:NSMakeRange(cap_len+4, address_lenth)];

        v.placefontlabel.zAttributedText=str;

        v.likesCountLabel.text = [NSString stringWithFormat:@"%i", [item[@"likes_count"] intValue]];
        if ([_device_lang_str isEqualToString:@"es"])
        {
            v.shightingsLabel.text = [NSString stringWithFormat:NSLocalizedString(@"%i Vistas", nil), [item[@"sightings_count"] intValue]];
        }
        else
        {
            v.shightingsLabel.text = [NSString stringWithFormat:NSLocalizedString(@"%i Sightings", nil), [item[@"sightings_count"] intValue]];
        }
        if ([nolocationstr isEqualToString:@"YES"])
        {
            v.distanceLabel.text =[NSString stringWithFormat:@"%.2f km", [item[@"distance"] floatValue]];
        }
        else
        {
            CLLocation *location1 = [[CLLocation alloc] initWithLatitude:[[item valueForKeyPath:@"spotting.place.lat"]floatValue] longitude:[[item valueForKeyPath:@"spotting.place.lng"] floatValue]];
            CLLocation *location2 = [[CLLocation alloc] initWithLatitude:[_explore_lat_str floatValue] longitude:[_explore_lng_str floatValue]];
            NSString *lat_laong=[NSString stringWithFormat:@"%f",[location1 distanceFromLocation:location2]];
            int km=[lat_laong floatValue]*0.001;
            NSString *distancestr=[NSString stringWithFormat:@"%d km",km];
            float dist=[distancestr floatValue];
            v.distanceLabel.text = [NSString stringWithFormat:@"%.2f km", dist];
        }

        if (![item[@"likes"] boolValue]) {
            v.likeButton.enabled = YES;
            v.likeButton.tag = idx;
            [v.likeButton setImage:[UIImage imageNamed:@"like_new.png"] forState:UIControlStateNormal];
            [v.likeButton addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        else
        {
            v.likeButton.tag = idx;
            [v.likeButton setImage:[UIImage imageNamed:@"like_new1.png"] forState:UIControlStateNormal];
        }

        NSArray *guides  = item[@"guides"];
        if ([guides count] > 0) {
            NSString *guideType = [[guides objectAtIndex:0] valueForKey:@"type"];
            UIImage *guidesIcon = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png", guideType]];
            v.guideButton.hidden = NO;
            v.guideButton.tag = idx;
            [v.guideButton setImage:guidesIcon forState:UIControlStateNormal];
            [v.guideButton addTarget:self action:@selector(guideAction:) forControlEvents:UIControlEventTouchUpInside];
        }
        v.shareButton.tag = idx;
        [v.shareButton addTarget:self action:@selector(shareAction:) forControlEvents:UIControlEventTouchUpInside];

        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoTapGesture:)];
        [v addGestureRecognizer:tapGesture];
        [tapGesture release];

        return [v autorelease];
    } else {
        UIImageView *iv = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"sc_img.png"]];
        UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        indicator.center = CGPointMake(CGRectGetMidX(iv.bounds), 110);
        [indicator startAnimating];
        [iv addSubview:indicator];
        [indicator release];
        return [iv autorelease];
    }


}

现在我的问题是:

如果我点击Like按钮,那么我必须更改按钮图像以及计数。我可以通过这种方法更改按钮图像

[sender setImage:[UIImage ImageNamed:@"image.png"]];

但是如何更改标签的Like计数?如何访问View的特定标签?我已经分配了标签但是,我不知道如何分配它。我不想重新加载整个滑块,因为照片是从网络(远程服务器)加载。谢谢你的时间。

1 个答案:

答案 0 :(得分:0)

只需保留对标签的引用并更新即可。

// @interface
@property (nonatomic, strong) UILabel *myLabel; 

// @implementation
_myLabel.text = @"Whatever.";

如果您有多个标签,请在创建视图时设置标记

newView.tag = sequenceNumber +100; 

然后更新

-(void)updateLabelWithTag:(NSInteger)tag {
   UILabel *label = (UILabel*) [self.scrollView viewWithTag:tag];
   label.text = @"Whatever.";
}