Xcode版本4.5 & IOS6
1. 開一個iPad的專案
2. 設定StoryBoard,並加入一個Tool Bar及一個Label
3. 加入一個UIViewController,並將BarItem拉一個Segue到這個UIView
4.新增一個class 檔,對應到步驟三的新UIView
5. 對UIView上的Class指定成此到新增的Class檔
6. 對mainAppDelegate.h設定公共變數
#import <UIKit/UIKit.h>
@interface mainAppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (nonatomic, retain) NSMutableArray *globalData;
@end
7. 對mainAppDelegate.m設定
#import "mainAppDelegate.h"
@implementation mainAppDelegate
@synthesize globalData;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
return YES;
}
...
8. 對主程式的mainViewController.h加入所需的變數
#import <UIKit/UIKit.h>
#import "mainAppDelegate.h"
@interface mainViewController : UIViewController
@property (weak, nonatomic) IBOutlet UILabel *valueLabel;
@property (nonatomic, retain) mainAppDelegate *appDelegate;
@property (nonatomic) NSTimer *timer;
@property (nonatomic) NSNumber *sliderValue;
- (void) showText;
@end
9. 對主程式的mainViewController.m加入所需的程式碼
#import "mainViewController.h"
@interface mainViewController ()
@end
#define TimeUnit 0.5 // 0.5sec
@implementation mainViewController
@synthesize sliderValue;
@synthesize timer;
@synthesize appDelegate;
@synthesize valueLabel;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
appDelegate = (mainAppDelegate *) [[UIApplication sharedApplication] delegate];
appDelegate.globalData = [[NSMutableArray alloc] init];
NSNumber *defaultValue = [NSNumber numberWithDouble:0.3];
[appDelegate.globalData addObject:defaultValue]; //0
[self initTimer];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) initTimer
{
// nowTime = lastTime;
if (timer == nil)
{
timer = [NSTimer scheduledTimerWithTimeInterval:TimeUnit target:self selector:@selector(showText) userInfo:nil repeats:YES];
}
}
- (void) showText
{
sliderValue = [appDelegate.globalData objectAtIndex:0];
valueLabel.text= [NSString stringWithFormat:@"Value = %2.3f",[sliderValue floatValue]] ;
}
@end
10.對Slider所在的UIView作設定,先針對SliderViewController.h
#import <UIKit/UIKit.h>
#import "mainViewController.h"
#import "mainAppDelegate.h"
@interface SliderViewController : UIViewController
@property (nonatomic, retain) mainAppDelegate *appDelegate;
@property (nonatomic) UISlider *mySlider;
@property (nonatomic) float myValue;
-(void) controlValueChanged;
@end
11.最後對SliderViewController. m加入控制碼
#import "SliderViewController.h"
@interface SliderViewController ()
@end
@implementation SliderViewController
@synthesize appDelegate;
@synthesize mySlider;
@synthesize myValue;
float lastvalue=0.5;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//特別說明,因為Popover再一次指定後,會重複執行此處。
- (void)viewDidLoad{
[super viewDidLoad];
// Do any additional setup after loading the view.
appDelegate = (mainAppDelegate *)[[UIApplication sharedApplication] delegate];
mySlider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 300, 50)];
[mySlider addTarget:self action:@selector(controlValueChanged) forControlEvents:UIControlEventValueChanged];
mySlider.minimumValue = 0;
mySlider.maximumValue = 3;
mySlider.continuous = YES;
// voiceSlider.value = 0.5; // 此種設定會讓Slider不能動
[mySlider setValue:lastvalue]; //只可以用此法,
[self.view addSubview:mySlider];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void) controlValueChanged
{
myValue = mySlider.value;
NSNumber *number = [NSNumber numberWithFloat:myValue];
[appDelegate.globalData replaceObjectAtIndex:0 withObject:number];
lastvalue = myValue;
}
@end
12. 執行結果,Label會即時顯示Slider上的變化。







沒有留言:
張貼留言