第一个iOS App RadioStation
同样来自Mitch, Bennett, Lees的教材,本人编译环境为Xcode 7
新建IOS Project, SingleView Application。
在Xcode环境中 - UI布局,添加UI对象,链接UI组件和UI对象。
UI布局:通过拖曳右下角的UI组件到Main.Storyboard中的空白Layout中
添加UI对象
本例中在ViewController的实例变量中添加了 IBOutlet类的各个UI组件的对象 - 对应三个动态Label,一个Slider
在类外面还定义了一个定义按钮动作的方法
定义好了之后右击对应的标签,将Reference Outlets一项拉出一条直线和对应的Label对象相连:
依次将要连接的动态标签,滑块,按钮都连接上。
之后再ViewController.m的viewDidLoad方法中添加动作,为按钮和滑块的动作添加实现:
// // ViewController.m // RadioStation // // Created by 王 颖豪 on 2/25/16. // Copyright © 2016 UTT Wangsta. All rights reserved. // #import "ViewController.h" #import "RadioStation.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. myStation = [[RadioStation alloc] initWithName:@"STAR 94" atFrequency:88.3]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)sliderDidChange:(id)sender { [myStation setFrequency:setFrequency.value]; } - (IBAction)buttonClick:(id)sender { [stationName setText:[myStation getName]]; [stationFrequency setText:[NSString stringWithFormat:@"%.1f", [myStation getFrequency]]]; if ([myStation getFrequency] >= [RadioStation minFMFrequency] && [myStation getFrequency] <= [RadioStation maxFMFrequency]) { [stationBand setText:@"FM"]; } else if( [myStation getFrequency] >= [RadioStation minAMFrequency] && [myStation getFrequency] <= [RadioStation maxAMFrequency]){ [stationBand setText:@"AM"]; } else { [stationBand setText:@"Invalid Band!"]; } } @end