80 likes | 161 Views
Chapter 5 Favorite Twitter Searches App. Interaction with buttons Load a web page. Access the web by code. Some apps access the web We can do that through code, i.e. programmatically Click on a button open up a web site. Buttons and Web Views. Create a Single View app
E N D
Chapter 5 Favorite Twitter Searches App • Interaction with buttons • Load a web page
Access the web by code • Some apps access the web • We can do that through code, i.e. programmatically • Click on a button open up a web site
Buttons and Web Views • Create a Single View app • In the view, place a button at the top and a web view occupying the rest of the window
Buttons and Web Views • Find button in Interface Builder • The corresponding class is UIButton • Find web view in Interface Builder • The corresponding class is UIWebView
Buttons and Web Views • In Controller.h, set up 2 instance variables to match the button and the web view IBOutlet UIButton *go; IBOutlet UIWebView *webView; • Connect the instance variable to the gui components in the window • Add a method goToYahoo -(IBAction) goToYahoo: (id) sender;
Buttons and Web Views • Code the method header of goToYahoo in Controller.m • Set up the event: when the user clicks on the button, goToYahoo will execute • Check that this is happening using a printf statement
Accessing and loading an URL • The NSURL class encapsulates a url; the method URLWithString enables us to build one from a string NSURL *url = [NSURL URLWithString:@”http://www.yahoo.com”]; • Now the UIWebView instance variable of the Controller class can load the web page
Accessing and loading an URL • NSURLRequest encapsulates a request to a server • The loadRequest method of UIWebView can load a web page based on a NSURLRequest [webView loadRequest : [NSURLRequest requestWithURL: url] ]; • The web page should load inside webView