从UIScrollview UIView SubView推送到另一个ViewController

时间:2014-05-20 18:48:42

标签: ios objective-c uiviewcontroller uiscrollview

我有一个以Carousel方式显示信息的滚动视图,我现在要做的是在旋转木马的每个视图上添加点击事件,我可以通过添加 UITapGestureRecongnizer *来做到这一点无论何时推送,它都会记录错误 *嵌套的推送动画可能会导致导航栏损坏,并且还会显示错误的信息 下面是我加载Carousel View并添加Click事件的代码,Carousel上的数据也来自 jSON ,并在 NSRutableArray *中以 NSObject 存储...

 -(void)updateUI:(NSMutableArray *)array {

CGFloat contentOffset = 0.0f;

for (NSString *dis in carouselArray) {
    CGRect frame = CGRectMake(contentOffset, 0.0f, responseScroll.frame.size.width, responseScroll.frame.size.height);

    UIView *views = [[UIView alloc] initWithFrame:frame];
    views.backgroundColor = [UIColor clearColor];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 130)];
    imageView.contentMode = UIViewContentModeScaleToFill;

    //imageView.image = [UIImage imageNamed:@"banner.png"];
    UITapGestureRecognizer *imageMove = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moreViewMove)];
    imageMove.cancelsTouchesInView = NO;

    NSString *urls = [dis valueForKey:@"imageURL"];
    if ([urls  isEqual: @""]) {
        imageView.image = [UIImage imageNamed:@"banner.png"];
    } else {
        [imageView setImageWithURL:[NSURL URLWithString:urls]];
    }

    UILabel *name = [[UILabel alloc] initWithFrame:CGRectMake(10, 132, 152, 21)];
    name.font = [UIFont fontWithName:@"Helvetica Bold" size:13];
    name.textColor = [UIColor blackColor];
    name.text = [dis valueForKey:@"name"];
    UILabel *address = [[UILabel alloc] initWithFrame:CGRectMake(10, 149, 194, 21)];
    address.font = [UIFont fontWithName:@"Helvetica Light" size:12];
    address.textColor = [UIColor blackColor];
    address.text = [dis valueForKey:@"address"];
    UILabel *km = [[UILabel alloc] initWithFrame:CGRectMake(278, 133, 42, 21)];
    km.font = [UIFont fontWithName:@"Helvetica" size:11];
    CLLocation *current = [[CLLocation alloc] initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude];
    CLLocation *itemLoc = [[CLLocation alloc] initWithLatitude:[[dis valueForKey:@"lat"] doubleValue] longitude:[[dis valueForKey:@"lon"] doubleValue]];
    CLLocationDistance itemDist = [itemLoc distanceFromLocation:current]/1000;
    //NSLog(@"Distance: %f", itemDist);
    km.text = [[NSString alloc] initWithFormat:@"%.2fkm", itemDist];
    views.userInteractionEnabled = YES;
    [views addGestureRecognizer:imageMove];
    _starRating = [[EDStarRating alloc] initWithFrame:CGRectMake(234, 149, 78, 16)];
    _starRating.starImage = [UIImage imageNamed:@"star.png"] ;
    _starRating.starHighlightedImage = [UIImage imageNamed:@"starhighlighted.png"];
    _starRating.maxRating = 5.0;
    _starRating.delegate = self;
    _starRating.horizontalMargin = 0;
    _starRating.editable=NO;
    _starRating.rating= [[dis valueForKey:@"rating"] floatValue];
    _starRating.displayMode=EDStarRatingDisplayHalf;
    [views addSubview:imageView];
    [views addSubview:name];
    [views addSubview:address];
    [views addSubview:km];
    [views addSubview:_starRating];
    [responseScroll setUserInteractionEnabled:YES];
    [responseScroll addSubview:views];

    [responseScroll addGestureRecognizer:imageMove];

    contentOffset += views.frame.size.width;
    responseScroll.contentSize = CGSizeMake(contentOffset, views.frame.size.height);
}

}

以下处理View Click Events的代码如下,我知道我做错了但我无法弄清楚该怎么做。

-(void)moreViewMove {


for (NSString *dat in carouselArray) {
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    path = [path stringByAppendingPathComponent:@"u_id.plist"];
    NSMutableDictionary *dico = [[NSMutableDictionary alloc] initWithContentsOfFile:path];

    NSString *place_id = [dat valueForKey:@"place_id"];
    NSString *place_reference = [dat valueForKey:@"place_reference"];

    CLLocation *current = [[CLLocation alloc] initWithLatitude:startLocation.coordinate.latitude longitude:startLocation.coordinate.longitude];
    CLLocation *itemLoc = [[CLLocation alloc] initWithLatitude:[[dat valueForKey:@"lat"] floatValue] longitude:[[dat valueForKey:@"lon"] floatValue]];
    CLLocationDistance itemDist = [itemLoc distanceFromLocation:current]/1000;

    UIDevice *device = [UIDevice currentDevice];
    NSString *u_id = [[device identifierForVendor] UUIDString];
    if ([dico objectForKey:@"u_id"]) {
        moreView *more = [self.storyboard instantiateViewControllerWithIdentifier:@"MoreView"];
        more.names = [dat valueForKey:@"name"];
        more.currentLat = [[NSString alloc] initWithFormat:@"%f", locationManager.location.coordinate.latitude];
        more.currentLon = [[NSString alloc] initWithFormat:@"%f", locationManager.location.coordinate.longitude];
        more.destinationLon = [dat valueForKey:@"lon"];
        more.destinationLat = [dat valueForKey:@"lat"];
        more.addressL = [dat valueForKey:@"address"];
        more.kilo = [[NSString alloc] initWithFormat:@"%.2fkm", itemDist];
        more.dataURL = @{@"type": @"details",@"u_id":[dico objectForKey:@"u_id"], @"place_id": place_id, @"place_reference": place_reference, @"device":@"server"};
        [self.navigationController pushViewController:more animated:YES];
    } 
}

}

请,任何帮助都会得到赞赏......谢谢

1 个答案:

答案 0 :(得分:0)

代码中最不寻常的部分是moreViewMove中的循环。看起来它会分配并推送到N个视图控制器,其中N是carouselArray中元素的数量。

这种事情的一般模式是有一个循环,可以根据模型创建视图,一个手势识别器来获取水龙头,以及将水龙头映射回模型的方法。然后,点击处理程序只查找单个模型项并执行导航。

updateUI:中的循环创建子视图和手势识别器。将视图映射回模型的常用方法是使用视图的tag属性。在您的updateUI中,您将执行以下操作:

UIView *views = [[UIView alloc] initWithFrame:frame];
views.tag = [carouselArray indexOfObject:dis];

// ...
// change the gesture recognizer selector to take a parameter
// notice the colon on the selector moreViewMove:
UITapGestureRecognizer *imageMove = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(moreViewMove:)];

现在在moreViewMove:中,您可以找出被点击的索引,因为手势识别器将作为参数发送...

- (void)moreViewMove:(UITapGestureRecognizer *)gr {

    // don't put a loop in here.
    // probably don't build a dictionary in here that doesn't depend on which item was tapped

    UIView *theViewThatWasTapped = gr.view;
    NSInteger tag = theViewThatWasTapped.tag;

    // now, we can know what part of our model was tapped
    NSString *dis = carouselArray[tag];

    // check how you're doing with an NSLog
    NSLog(@"user tapped view with tag %ld model is %@", tag, dis);

    // your job now is to allocate a single view controller,
    // configure it with your model (dis) and push it

尽可能少地配置新的vc。关于模型的哪个部分被选中不变的任何初始化东西应该被移动到其他地方,希望进入新的视图控制器的初始化。

相关问题