2011-04-25 56 views
2

我在製作滑塊應用程序。爲此,我寫了下面的代碼,但是當我按下build和go選項時,滑塊不移動。它在.h和.m文件中給出錯誤。iphone滑塊示例

SliderAppDelegate.h

#import <UIKit/UIKit.h> 

@interface SliderAppDelegate : NSObject <UIApplicationDelegate> 
{ 
    UIWindow *window; 
    IBOutlet UISlider *slider; 
    IBOutlet UILabel *labelTxt; 
} 

@property (nonatomic, retain) IBOutlet UIWindow *window; 
@property (nonatomic, retain) UILabel *labelTxt; 

-(IBAction)changeSlider:(id)sender ; 
@end 

SliderAppDelegate.m

#import "SliderAppDelegate.h" 

@implementation SliderAppDelegate 

@synthesize window,labelTxt ; 

- (void)applicationDidFinishLaunching:(UIApplication *)application 
{ 
    [window makeKeyAndVisible]; 
} 

-(IBAction)changeSlider:(id)sender 
{ 
    labelTxt .text= [[NSString alloc] initWithFormat:@" Value %d ", (int)slider.value]; 
} 

- (void)dealloc 
{ 
    [window release]; 
    [label.Txt release]; 
    [super dealloc]; 
} 

@end 

是否有這個代碼的任何問題?

+2

您收到了什麼錯誤?嘗試刪除'.text'屬性之前的空格。並且請將所有代碼放在代碼標籤中。工具欄中有一個按鈕。 – FeifanZ 2011-04-25 10:26:05

+0

沒有代碼標籤,你用4個空格縮進每行。 – 2011-04-25 10:30:04

+1

哪個事件你用uislider在界面中設置? – GameLoading 2011-04-25 10:30:49

回答

2

正如@ Inspire48建議作出修改了代碼,並確保IBOutlets和IBActions正確連接。同時檢查滑塊的動作是否連接到事件「值已更改」。 您也可以將您的代碼更改爲此

-(IBAction)changeSlider:(id)sender 

{ 

    labelTxt.text= [NSString stringWithFormat:@" Value %d ", (int)slider.value]; 

} 
1

正如Inspire48的評論中指出的那樣,刪除labelTxt .text中的空格並將[label.Txt release];更改爲[labelTxt release];

您在changedSlider方法中有內存泄漏。初始化的字符串不被釋放。

也許你應該閱讀一本關於目標C.