這是我第一次編寫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函數時設置的值?
你爲什麼使用VIDEOQUALITY指針?此外,爲什麼它全部上限 –
@RichTolley:是的,我改變了它,但問題仍然是一樣的。調用setVideoQual函數,並調用viewDidLoad函數,就像VIDEOQUALITY返回0。 –
是否有其他方法更改變量? –