这是我第一次编写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。 –
是否有其他方法更改变量? –