2012-03-25 95 views
0

我有一个视图控制器中的视图集合。通过触摸其中一个视图,我想加载特定的数据,例如。将网页转换为网页视图,并在同一个视图控制器上如何通过触摸UIView将内容加载到WebView中

您将如何实现该目标?

预先感谢您

这里是我的代码不想工作:

UIView *categoryTitle = [[UIView alloc] initWithFrame:CGRectMake(0, 166 * counter 
                    , 500, 20)]; 

    UILabel *categoryLabel = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, 200, 20)]; 

    [categoryLabel setBackgroundColor:[UIColor clearColor]]; 
    NSMutableArray *allCurrentNews = [[News alloc] allNewsFromCategory:cat.CategoryId]; 

    categoryLabel.text = cat.Name; 
    categoryLabel.textColor = [UIColor whiteColor]; 

    [categoryTitle addSubview:categoryLabel]; 

    UIColor *myblack = [[UIColor alloc] initWithRed:0.14 green:0.14 blue:0.14 alpha:1]; 
    UIColor *ligheterBlac = [[UIColor alloc] initWithRed:0.227 green:0.22 blue:0.22 alpha:1]; 
    [categoryTitle setBackgroundColor:myblack]; 

    UIScrollView *tempScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 166 * counter, 500, 166)]; 

    UIColor *tempcolor = ligheterBlac; 
    tempScroll.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor; 
    tempScroll.layer.borderWidth = 0.5f; 
    int countnews = 0; 

    for (News *news in allCurrentNews) 
    { 
     UIView *newsContainer = [[UIView alloc] initWithFrame:CGRectMake(160 * countnews, 30, 156, 126)]; 
     newsContainer.tag = countnews + 1; 
     [newsContainer addGestureRecognizer:recognizer]; 

     //newsContainer.NewsId = news.NewsId; 
     LazyImageView *image = [[LazyImageView alloc] initWithURl:[NSURL URLWithString:news.Thumbnail]]; 
     image.frame = CGRectMake(0, 0 , 156, 96); 
     UILabel *newsTitle = [[UILabel alloc] initWithFrame:CGRectMake(0, 96, 156, 30)]; 
     newsTitle.backgroundColor = myblack; 
     newsTitle.numberOfLines = 2; 
     newsTitle.font = [UIFont systemFontOfSize:11]; 
     newsTitle.text = news.Title; 
     newsTitle.textColor = [UIColor whiteColor]; 
     newsTitle.textAlignment = UITextAlignmentCenter; 

     newsContainer.layer.borderColor = [UIColor colorWithRed:0.34 green:0.34 blue:0.34 alpha:1].CGColor; 
     newsContainer.layer.borderWidth = 0.5f; 

     [newsContainer addSubview:image]; 
     [newsContainer addSubview:newsTitle]; 

     countnews ++; 
     [tempScroll setContentSize:CGSizeMake(allCurrentNews.count * 156, 96)]; 
     [tempScroll addSubview:newsContainer]; 
     //[image release]; 
    } 

    [tempScroll setBackgroundColor: tempcolor]; 

    [categories addSubview:tempScroll]; 
    [categories addSubview:categoryTitle]; 
    [tempcolor release]; 
    [tempScroll release]; 
    counter ++; 
} 

self.detailsView.layer.masksToBounds = NO; 
self.detailsView.layer.shadowOffset = CGSizeMake(-10, 5); 
self.detailsView.layer.shadowRadius = 5; 
self.detailsView.layer.shadowOpacity = 0.3; 

[self.view addSubview:categories]; 
[self.view addSubview:_detailsView]; 
[self.view addSubview:MainSubTitle]; 
[self.view addSubview:MainTitle]; 

回答

0

使用UITapGestureRecognizer

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:selector(processTap:)]; 
    UIView *targetView = //assign the view you need 
    targetView.tag = 5; 
    [targetView addGestureRecognizer:recognizer]; 
} 

- (void)processTap:(UITapGestureRecognizer *)recognizer { 
    UIView *view = recognizer.view; 
    if (view.tag == 5) { 
    } 
} 
+0

我得到使用未声明的标识符“processTap” 和“功能选择的隐式声明是无效的C99” – Alexidze 2012-03-25 21:59:15

+0

那么这是非常基本的东西。添加这一行 - (void)processTap:(UITapGestureRecognizer *)识别器;到您的.h文件或到.m文件中的专用扩展名。 – Eugene 2012-03-25 22:39:49

+0

我做了一切,但它仍然没有被称为 – Alexidze 2012-03-26 06:16:36