如何在iOS中的UIWebView中添加添加UIButton

时间:2016-01-27 11:30:51

标签: ios objective-c uiwebview

我有一点怀疑..我正在基于json响应动态显示uiwebview。我需要在uiwebview中添加uibutton。所以我在uiwebview中添加了uibutton。但是uibutton应该只展示第一个uiwebview。剩下的网页浏览量不会显示uibutton.i将uiview作为超级视图。和uiwebview添加uiview的子视图。最后uibutton正在添加uiwebview的子视图。我正在写这样的代码。帮助我任何身体..

UIView *view = (UIView *)[self.scrollView viewWithTag:[[response valueForKey:@"DatacardId"] integerValue]];

UIWebView *webview = (UIWebView *)[view viewWithTag:WEBVIEW_TAG];

//Enable/Disable previous and next button based on page index and pagecount

[self presentNextAndPreviousButtonsBasedOnResponse:response];

self.isCardsInitialization = TRUE;

button1 = [UIButton buttonWithType:UIButtonTypeCustom];

[button1 addTarget:self action:@selector(aMethod)
  forControlEvents:UIControlEventTouchUpInside];

[button1 setTitle:@"Show View" forState:UIControlStateNormal];

[button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

button1.frame = CGRectMake(120.0, 50.0, 160.0, 40.0);

int index = (int)[[webview subviews] indexOfObject:button1];

NSLog(@"index value of button is:%d",index);

[self webViewDidFinishLoad:webview];

[webview addSubview:button1];

1 个答案:

答案 0 :(得分:0)

以下是我对您的要求的理解:

  1. 您需要在第一次加载时为UIWebView添加按钮
  2. UIWebView上加载的所有未来页面上,应删除该按钮
  3. 当用户点击按钮时,应将其删除
  4. 在基于json响应的UIWebView的每次首次加载时,都应显示按钮。
  5. <强>解决方案

    在类Extension中添加BOOL变量以用作标志,以确定是否显示该按钮。

    @property BOOL displayButton;
    

    UIWebViewDelegate中设置displayButtonviewDidLoad属性并初始化UIButton

    - (void)viewDidLoad {
        [super viewDidLoad];
    
        UIWebView *webview = (UIWebView *)[self.view viewWithTag:WEBVIEW_TAG];
        webview.delegate = self;
    
        if (!self.button1) {
    
            // Initialize the button
            self.button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    
            [self.button1 addTarget:self action:@selector(aMethod)
                   forControlEvents:UIControlEventTouchUpInside];
    
            [self.button1 setTitle:@"Show View" forState:UIControlStateNormal];
    
            [self.button1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            [self.button1 setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    
            self.button1.frame = CGRectMake(120.0, 50.0, 160.0, 40.0);
        }
    
        _displayButton = YES;
    }
    

    通过设置webview的委托,您无需手动调用webViewDidFinishLoad:,因为webview实际完成加载时会调用webViewDidFinishLoad:。 (因为它被设计为)

    UIButton中,如果首次加载webview,您可以添加- (void)webViewDidFinishLoad:(UIWebView *)webView { if (_displayButton) { // Add the button to webview [webView addSubview:self.button1]; // Set _displayButton to No, to avoid button being added in future loads _displayButton = NO; } else { // Remove the button [self.button1 removeFromSuperview]; } } ,如果是后续加载,则可以删除该按钮。

    removeFromSuperview

    要点击按钮,只需在aMethod中调用- (void)aMethod { //Remove the button from superview when it is tapped [self.button1 removeFromSuperview]; } (选择器添加到初始化按钮)

    _displayButton = YES;

    每当您在UIWebView

    中加载新回复时,您都需要设置Private Sub Worksheet_Activate() If ActiveCell.Value = "OK" Then 'Here ActiveCell is any cell that is active, 'but you say, that you want to test the cells of 'a particular column, in that case you could use 'Opt 01: 'If ActiveCell.Column = 1 Then 'Here you are asking for the column 'for the ActiveCell ' use a nested if 'If ActiveCell.Value = "OK" Then ' here if the activecell is in the ' right column and is with the right 'value, you get the rows colored 'Range("A13:E13").Select 'Well you are using this... but you say 'that you want to color the row of the corresponding cell 'in that case you can use this: Dim r Dim rng As Range r = ActiveCell.Row Set rng = Range(Cells(r, 1), Cells(r, 20)) 'you store inside rng the cells: 'from column 1 = A, Row x 'to column 20 = T, row x ' where x is the row of the activecell 'you can change the column as you need With rng.Interior 'here you can use rng instead selection 'and you affect the cells without selecting them. .ColorIndex = 4 .Pattern = xlSolid End With End If End Sub