今天练习的主要目标:
1。 使用UIwebView的控件,如何加载远程服务器上的数据
2。 使用UISegmentedControl控件,选择不同段的数值
首先看下练习的最终效果
练习具体步骤:
1. 首先创建一个基于view_based_application, 项目名称为FlowerWebViewControl
2. 然后,打开FlowerWebViewControl.h
添加具体的ui控件
IBOutlet UISegmentedControl *colorChoice;
IBOutletUIWebView *flowerView;
IBOutletUIWebView *detailView;
@property(nonatomic,retain)UISegmentedControl *colorChoice;
@property(nonatomic,retain)UIWebView *flowerView;
@property(nonatomic,retain)UIWebView *detailView;
同时增加一个函数声明
-(IBAction)getFlower:(id)sender;
3. 设计界面
4. 实现UI控件、函数和File's Owner的链接
关于连接的小技巧:
a. 连接UI的方法,
选中File's Owner, 按住control, 画出连线到具体的ui控件(只要在.h中声明过的,就会出现在可供选择的ui列表中),注意不要连接错误
b. 连接控件处理处理函数的方法:
选择具体的UI控件,按住Control, 画出连线到File's Owner, 在下拉列表中选择正确的函数名称,这样控件就和具体的处理函数连接上了。
5. 关于具体控件的处理
a. uisegmentedcontrol,
获取具体数值的方法是:
color = [colorChoicetitleForSegmentAtIndex:colorChoice.selectedSegmentIndex];
b. webView加载html页面的具体方法首先NSURL使用urlString初始化
然后 webView loadRequestWithUrl: nsurl
具体代码如下
//
// FlowerWebViewControllerViewController.h
// FlowerWebViewController
//
// Created by 旭 陈 on 11-12-3.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface FlowerWebViewControllerViewController : UIViewController {
IBOutlet UISegmentedControl *colorChoice;
IBOutlet UIWebView *flowerView;
IBOutlet UIWebView *detailView;
// IBOutlet UISwitch *mySwitch;
//2webView
//segmentedControll
}
@property(nonatomic,retain)UISegmentedControl *colorChoice;
@property(nonatomic,retain)UIWebView *flowerView;
@property(nonatomic,retain)UIWebView *detailView;
//@property(nonatomic,retain)UISwitch *mySwitch;
-(IBAction)getFlower:(id)sender;
-(IBAction)toggleFlowerDetail:(id)sender;
//getFlower
//toggleFlowerDetail
@end
//
// FlowerWebViewControllerViewController.m
// FlowerWebViewController
//
// Created by 旭 陈 on 11-12-3.
// Copyright 2011 __MyCompanyName__. All rights reserved.
//
#import "FlowerWebViewControllerViewController.h"
@implementation FlowerWebViewControllerViewController
//@synthesize mySwitch;
@synthesize flowerView;
@synthesize detailView;
@synthesize colorChoice;
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
-(IBAction)toggleFlowerDetail:(id)sender{
detailView.hidden = ![sender isOn];
}
-(IBAction)getFlower:(id)sender{
NSLog(@"getFlower");
NSURL *imageUrl;
NSURL *detailUrl;
NSString *imageUrlStr;
NSString *detailUrlStr;
NSString *color;
int sessionID;
color = [colorChoice titleForSegmentAtIndex:colorChoice.selectedSegmentIndex];
NSLog(@"%@",color);
sessionID = random()%10000;
imageUrlStr= [[NSString alloc]initWithFormat:@"http://www.floraphotographs.com/showrandomiphone.php?color=%@&session=%d",color,sessionID];
detailUrlStr = [[NSString alloc]initWithFormat:@"http://www.floraphotographs.com/detailiphone.php?session=%d",sessionID];
imageUrl = [[NSURL alloc]initWithString:imageUrlStr];
detailUrl = [[NSURL alloc] initWithString:detailUrlStr];
[flowerView loadRequest:[NSURLRequest requestWithURL:imageUrl]];
[detailView loadRequest:[NSURLRequest requestWithURL:detailUrl]];
detailView.backgroundColor = [UIColor clearColor];
[imageUrlStr release];
[detailUrlStr release];
[imageUrl release];
[detailUrl release];
}
@end
这个例子完成了,关键的技术点
1。 ui的声明、界面添加和连接
2。segmentedControll的数值获取
3。 webview的页面加载nsUrl, webView loadRequest:nsUrl
如果需要工程代码的,请留下邮箱地址。