[英]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.