[英]Load url selected object in UIWebView (iOS)
我想使用基于数组中自定义对象的索引的URL打开UIWebView。 当我点击一个按钮时,基于数组中对象的索引,我需要UIWebView打开该特定的URL。 问题是,当我打开webview时,它将仅加载数组中第一个对象的URL(索引为0)。 这是我的代码:
.m:
//这是我的viewDidLoad方法的摘要。
-(void)viewDidLoad{
[super viewDidLoad];
buyButton = [[UIButton alloc]init];
buyButton = [UIButton buttonWithType:UIButtonTypeCustom];
buyButton.frame = CGRectMake(228, 8, 25, 25);
[buyButton setBackgroundImage:[UIImage imageNamed:@"buy-now.png"] forState:UIControlStateNormal];
[buyButton addTarget:self action:@selector(buyBttnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buyButton];
int count = [self->myArray count];
for (int index = 0; index < count; index++)
{
DomainModel *eachObject = [self->myArray objectAtIndex:buyButton.tag];
buyButton.tag = index;
urlString = [NSString stringWithFormat:@"%@",eachObject.url];
NSLog(@"Selected index: %d",urlString);
}
-(void)buyBttnPressed:(id)sender{
UIWebView *buyView = [[UIWebView alloc] initWithFrame:CGRectMake(9,132,305,368)];
buyView.backgroundColor = [UIColor whiteColor];
buyView.scalesPageToFit = YES;
buyView.delegate = self;
[self.view addSubview:buyView];
UIButton *cancelBttn = [[UIButton alloc]init];
cancelBttn = [UIButton buttonWithType:UIButtonTypeCustom];
cancelBttn.frame = CGRectMake(3, 5, 25, 25);
[cancelBttn setBackgroundImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
[buyView addSubview:cancelBttn];
[buyView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlString]]];
}
如何修复循环,以便在按下buyBttn时,将加载与关联索引关联的网址?
看来,如果您在buyBtnPressed上调用此代码,那么按钮的TAG将会始终设置为self.myArray.count -1(您正在循环播放,但始终将其设置在同一按钮上)。 因此当前将始终指向同一项目-列表中的最后一项。 我认为这:
int i = 0;
for (MyObject *obj in self->myArray)
{
i++;
btn.tag = i;
}
应该在viewDidLoad或填充数组的任何位置完成。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.