I would like to solve the problem of array from URLs. I use JSON in my app and I have an array of cells. My goal: if you click in any cell, page will open in Safari with URL from JSON array.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier =@"LocationCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: cellIdentifier];
Location *location = [_locations objectAtIndex:indexPath.row];
//...
myCell.userInteractionEnabled = YES;
UITapGestureRecognizer *gestureRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(openUrl:)];
gestureRec.numberOfTouchesRequired = 1;
gestureRec.numberOfTapsRequired = 1;
[myCell addGestureRecognizer:gestureRec];
return cell;
}
- (void) openUrl: (id)sender: (Location *) location {
UIGestureRecognizer *rec = (UIGestureRecognizer *)sender;
id hitLabel = [self.view hitTest:[rec locationInView:self.view] withEvent:UIEventTypeTouches];
if ([hitLabel isKindOfClass:[UILabel class]]) {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: location.url]];
}
}
Something wrong, because the app crashes with this error:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[ViewController openUrl:]: unrecognized selector sent to instance 0x7fd3a850ad40'
But if I write in UITableViewCell this:
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: location.url]];
links will open in Safari immediately when the application starts. So I think that JSON parsing is correct.
openURL:message to whatever class is yourViewControllerclass rather than to yourUIApplication.