这是我第一次使用Iphoneapp进行编码,而且我在弄清楚我在做错了什么时遇到了一些困难。
我有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中的我有一个名为VIDEOQUALITY的属性,我想将其设置为从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 :(得分:1)
为什么要将NSInteger* VIDEOQUALITY;
声明为指针?您好像不需要这样,因为NSInteger
是原始类型,并且被定义为无符号int NSUInteger
(例如)。
我想如果你把你的声明改为
{
NSInteger VIDEOQUALITY;
}
...
@property (nonatomic,assign) NSInteger VIDEOQUALITY;
和实施到
-(void)setVideoQual:(NSInteger)quality
{
NSLog(@"SETTING VIDEO QUALITY %ld",quality);
VIDEOQUALITY=quality;
}
你会得到理想的行为。