objective c - how to add Add UIButton in a UIWebView in iOS -


i have small doubt.. displaying uiwebview dynamically based on json response. need add uibutton in uiwebview. adding uibutton in uiwebview. uibutton should display first uiwebview. remaining webviews not display uibutton.i took uiview superview. , uiwebview adding subview of uiview. , uibutton adding subview of uiwebview .i writing code this. me body..

uiview *view = (uiview *)[self.scrollview viewwithtag:[[response valueforkey:@"datacardid"] integervalue]];  uiwebview *webview = (uiwebview *)[view viewwithtag:webview_tag];  //enable/disable previous , next button based on page index , 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]; 

here understanding of requirement:

  1. you need add button uiwebview first time loads
  2. on future pages loaded on uiwebview, button should removed
  3. when user taps button, should removed
  4. on every first load of uiwebview based on json response, button should displayed.

solution

add bool variable in class extension used flag determine if button displayed or not.

@property bool displaybutton; 

set uiwebviewdelegate , displaybutton property in viewdidload , initialize uibutton

- (void)viewdidload {     [super viewdidload];      uiwebview *webview = (uiwebview *)[self.view viewwithtag:webview_tag];     webview.delegate = self;      if (!self.button1) {          // initialize 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; } 

by setting delegate of webview, not need call webviewdidfinishload: manually called when webview completes loading. (as designed be)

in webviewdidfinishload:, can add uibutton if webview loaded first time, or remove button if subsequent load.

- (void)webviewdidfinishload:(uiwebview *)webview {      if (_displaybutton) {          // add button webview         [webview addsubview:self.button1];          // set _displaybutton no, avoid button being added in future loads         _displaybutton = no;      } else {          // remove button         [self.button1 removefromsuperview];     } } 

to remove button when tapped call removefromsuperview in amethod (selector added button on intialization)

- (void)amethod {      //remove button superview when tapped     [self.button1 removefromsuperview]; } 

you need set _displaybutton = yes;, whenever load fresh response in uiwebview


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -