ios - Loading NSURLRequest on webView from a different class. -
i created view controller webview in storyboard called googlesearchcontroller. property webview was, of course, declared in googlesearchcontroller.
i attempting access synthesized webview property, *googlewebview, within mapviewcontroller, can append point annotations google search. output here correct. i'm getting appended , desired url, i'm failing load it. @ end of leftbuttonannotationpressed method, notice try call loadrequest on self.classobj.googlewebview. not going should. first time i've tried use @synthesize, , i'm open suggestions.
my question comes in 2 parts. a: using @synthesize intended? , b: if proper use, why won't webview load request?
googlesearchcontroller.h
@interface googlesearchcontroller : uiviewcontroller <uiwebviewdelegate> @property (strong, nonatomic) iboutlet uiwebview *googlewebview; @end
googlesearchcontroller.m
@synthesize googlewebview; - (void)viewdidload { [super viewdidload]; self.googlewebview = [[uiwebview alloc] init]; self.googlewebview.delegate = self; }
mapviewcontroller.m
@property (nonatomic, strong) googlesearchcontroller *classobj; -(void)leftbuttonannotationpressed:(uibutton *)sender { [self performseguewithidentifier:@"googlesearch" sender:nil]; self.classobj = [[googlesearchcontroller alloc] init]; <-------- mkpointannotation *annotation = [self.mapview.selectedannotations objectatindex:([self.mapview.selectedannotations count]) -1]; nsstring *appendstring = annotation.title; nsstring *googlestring = @"http://www.google.com/search?q="; nsstring *appendedurlstring = [googlestring stringbyappendingstring:appendstring]; if ([appendedurlstring containsstring:@" "]) { appendedurlstring = [appendedurlstring stringbyreplacingoccurrencesofstring:@" " withstring:@"+"]; nsurl *url = [nsurl urlwithstring:appendedurlstring]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; [self.classobj.googlewebview loadrequest:request];<------ nslog(@"appended string: %@", request); }
@sythesize
not required/mandatory anymore. without @synthesize
ios/xcode automatically create instance variable _[propertyname]
if uiwebview created in ib, need not initialize programatically. make sure webview , viewcontroller iboutlet property connected. should suffice.
the important reason can think request not loading because loading url in different instance 1 displayed in googlesearchviewcontroller
you doing this
[self performseguewithidentifier:@"googlesearch" sender:nil]; self.classobj = [[googlesearchcontroller alloc] init]; <--------
in first line, asking storyboard perform segue. so, story board create instance of view controller views , subviews , display.
in second line, creating own instance of view controller.. displaying it? -- no one.
your loading url in wrong instance.
ok now, how gain access view controller being displayed?
xcode/ios gives option prepareforsegue
can access vc , webview shown below load request.
- (void)prepareforsegue:(uistoryboardsegue *)segue sender:(id)sender { if([segue.identifier isequaltostring:@"googlesearch"]){ googlesearchviewcontroller *vc = nil; if([segue.destinationviewcontroller iskindofclass:[uinavigationcontroller class]]){ uinavigationcontroller *navvc = (uinavigationcontroller *)segue.destinationviewcontroller; vc = (googlesearchviewcontroller *)navvc.viewcontrollers[0]; }else{ vc = segue.destinationviewcontroller; } if(vc){ nsurl *url = [nsurl urlwithstring:appendedurlstring]; nsurlrequest *request = [nsurlrequest requestwithurl:url]; [vc.googlewebview loadrequest:request]; } } }
would interested know if worked you
Comments
Post a Comment