2011/07/11

使用UISegmentedControl切換網頁



.h
1
2
3
4
5
@interface SecondViewController : UIViewController {
IBOutlet UIWebView *stockTrend;
IBOutlet UISegmentedControl *segmentControl;
}
- (IBAction)segmentControlAction:(id)sender;


.m
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
- (IBAction)segmentControlAction:(id)sender {

NSInteger segment = segmentControl.selectedSegmentIndex;

if (segment == 0) {
[stockTrend loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"網址1"]]];
}
else if (segment == 1) {
[stockTrend loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"網址2]]];
}
}

重點在xib,除了拉Outlets外,Actions要拉Segmented Control Value Changed,資料才會跟著動,很重要!