简体   繁体   English

如何通过触摸UIView将内容加载到WebView中

[英]How to load content into a WebView by touching a UIView

I Have a collection of views in a view controller. 我在视图控制器中有一组视图。 by touching one of those views I would like to load specific data, for ex. 通过触摸这些视图之一,我想加载特定数据,例如。 a webpage into a web view that is on the same view controller 将网页转换为同一视图控制器上的Web视图

How would you accomplish that? 您将如何实现?

Thank you in advance 先感谢您

Here is my code with does not want to work: 这是我的代码,不想工作:

    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];

Use the UITapGestureRecognizer . 使用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) {
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM