2016-02-29 71 views
-1

這是我第一次編寫Iphoneapp,並且在確定我在這裏做錯了什麼時遇到了一些困難。ios - 無法從其他視圖控制器訪問數據

我有2個視圖控制器:viewController和viewController2我從viewController調用viewcontroller2來設置一些參數並在viewController中訪問它們。

我使用委託模式對於這一點,如下圖所示:

在viewController2.h

#import <UIKit/UIKit.h> 

@protocol ViewController2Delegate 
-(void)setVideoQual:(NSInteger)quality; 
@end 


@interface ViewController2 : UIViewController 


@property (nonatomic, retain) id delegate; 
@property IBOutlet UISegmentedControl *videoQuality; 

-(IBAction)handleCloseButton:(id)sender; 
-(IBAction)updateVideoQuality:(UISegmentedControl *)sender; 

@end 

基本上,我想訪問我設定的viewController中的功能setVideoQual質量。我使用UISegmentedControl設置質量。

在viewController.m

#import "ViewController2.h" 

@implementation ViewController2 
@synthesize delegate; 
-(IBAction)updateVideoQuality:(UISegmentedControl *)sender 
{ 
    NSLog(@"change video quality: %ld", (long)sender.selectedSegmentIndex); 


} 
-(IBAction)handleCloseButton:(id)sender 
{ 
    [self.delegate setVideoQual:_videoQuality.selectedSegmentIndex]; 
    [self.navigationController popViewControllerAnimated:YES]; 

} 
@end 

在viewController.h我有我想設置從viewController2進口質量屬性命名視頻質量:

viewController.h

#import <UIKit/UIKit.h> 
#import "ViewController2.h" 


@interface ViewController : UIViewController 
{ 
... 
NSInteger VIDEOQUALITY; 
} 

... 

@property (nonatomic,assign) NSInteger VIDEOQUALITY; 


@end 

然後在viewController.m我有:

@implementation ViewController 
@synthesize VIDEOQUALITY; 


#pragma mark - UI Actions 

- (IBAction)actionStart:(id)sender; 
{ 
    ... 
    NSLog(@"NEW VIDEO QUALITY: %d", VIDEOQUALITY); 
    ... 

} 
-(void)setVideoQual:(NSInteger)quality 
{ 
    NSLog(@"SETTING VIDEO QUALITY %ld",quality); 
    VIDEOQUALITY=quality; 
    } 


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender 
{ 
    ViewController2 *ViewController2 = [segue destinationViewController]; 
    ViewController2.delegate = self; 
} 
@end 

我不明白爲什麼當我調用actionStart時,VIDEOQUALITY的值永遠不會設置爲它在調用setvideoQual函數時設置的值?

+0

你爲什麼使用VIDEOQUALITY指針?此外,爲什麼它全部上限 –

+0

@RichTolley:是的,我改變了它,但問題仍然是一樣的。調用setVideoQual函數,並調用viewDidLoad函數,就像VIDEOQUALITY返回0。 –

+0

是否有其他方法更改變量? –

回答

1

爲什麼你聲明NSInteger* VIDEOQUALITY;作爲指針?看起來你不需要這個,因爲NSInteger原始的類型,並被定義爲無符號的​​(例如)。 我認爲,如果你改變你的聲明

{ 
    NSInteger VIDEOQUALITY; 
} 
... 

@property (nonatomic,assign) NSInteger VIDEOQUALITY; 

和實施

-(void)setVideoQual:(NSInteger)quality 
{ 
    NSLog(@"SETTING VIDEO QUALITY %ld",quality); 
    VIDEOQUALITY=quality; 
} 
你會得到期望的行爲

+0

問題仍然是,setVideoQual被調用後,調用viewdidload函數,然後我不知道爲什麼VIDEOQUALITY回到0 ... –

+0

@RickyBobby在handleCloseButton:你調用的方法'[self.delegate setVideoQual: _videoQuality.selectedSegmentIndex];'這就是爲什麼你在VIDEOQUALITY變量中得到0(如果選擇了第一個控制段)。你通過這段代碼試圖達到什麼目的? – curious

+0

即使我選擇另一個索引,我仍然得到0,我無法弄清楚爲什麼。 –

相關問題