cell.m不能用[self.navigationController pushViewController:show animated:YES];这个方法进入下一个控制器视图。
最后用协议实现。当用户点击一个图片的时候,进入到cell类的iCarousel代理方法-(void)carousel:(iCarousel *)carousel
didSelectItemAtIndex:(NSInteger)index在这里把index传给StarShowViewController,然后在ViewController里面做push的动作。
(1)定义协议ImageItemDelegate.h
#import <Foundation/Foundation.h>
@protocol ImageItemDelegate <NSObject]]]]>
-(void)passItem:(NSString*)index;//把点击的是第几个image 的index 传过来
@end
@protocol ImageItemDelegate <NSObject]]]]>
-(void)passItem:(NSString*)index;//把点击的是第几个image 的index 传过来
@end
(2)在ViewController.h类里面遵守协议
#import "ImageItemDelegate.h"
@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource,ImageItemDelegate>{
UITableView *starTableView;
NSMutableArray *starArray;
}
@interface ViewController : UIViewController <UITableViewDelegate,UITableViewDataSource,ImageItemDelegate>{
UITableView *starTableView;
NSMutableArray *starArray;
}
(4)在TableViewCell.h类里面声明一个代理
#import "ImageItemDelegate.h"
@interfaceTableViewCell : UITableViewCell <iCarouselDataSource,iCarouselDelegate,UIGestureRecognizerDelegate]]]]>
@property(nonatomic,retain)NSObject <ImageItemDelegate> *itemDelegate;//声明一个代理
@interfaceTableViewCell : UITableViewCell <iCarouselDataSource,iCarouselDelegate,UIGestureRecognizerDelegate]]]]>
@property(nonatomic,retain)NSObject <ImageItemDelegate> *itemDelegate;//声明一个代理
(5)在TableViewCell.m类里面传值
//选择某个image
item
-(void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{
NSLog(@"index:%ld",(long)index);
NSString *str = [NSString stringWithFormat:@"%ld",index];
UIView *view = carousel.currentItemView;
[self.itemDelegate passItem:str];//通过代理把str传到StarShowViewController类里面
}
-(void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index{
NSLog(@"index:%ld",(long)index);
NSString *str = [NSString stringWithFormat:@"%ld",index];
UIView *view = carousel.currentItemView;
[self.itemDelegate passItem:str];//通过代理把str传到StarShowViewController类里面
}
(6)ViewController.m 里面 表的代理方法 - (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath设置代理
cell.itemDelegate = self;//设置代理
(7)进入到下一个viewcontroller
#pragma mark - ImageItemDelegate
//从cell那边拿到值
-(void)passItem:(NSString *)index{
yViewController *y = [[yViewController alloc] init];
y.imageIdex = index;
[self.navigationController pushViewController:y animated:YES];
}
//从cell那边拿到值
-(void)passItem:(NSString *)index{
yViewController *y = [[yViewController alloc] init];
y.imageIdex = index;
[self.navigationController pushViewController:y animated:YES];
}
----------------------
叫你好好看书,又不好好看书! 实现方法很多种:①、可以在controller里面直接给Image添加点击事件 ②、使用delegate,在当前controller里面实现即可触发点击事件 ③ block 简单 ④ 事件响应者链可以处理
第一种在这里不合适,第三种在这里还没有用过,理论上block也可以传值,考虑到block限制比较多,不使用 第四种事件响应者链不太清楚是怎么回事