简体   繁体   中英

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

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 .

- (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) {
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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