这是我第一次编写Iphone应用程序,我很难弄清楚我在这里做错了什么。
我有 2 个视图控制器:viewController 和 viewController2 我从 viewController 调用 viewcontroller2 来设置一些参数并在 viewController 中访问它们。
我为此使用委托模式,如下所示:
在视图中控制器2.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 时,视频质量的值永远不会设置为调用 setvideoQual 函数时似乎设置为的值?
为什么你声明NSInteger* VIDEOQUALITY;
是指针?似乎你不需要这个,'因为NSInteger
是基元类型,被定义为无符号int NSUInteger
(例如)。我想如果你把你的声明改为
{
NSInteger VIDEOQUALITY;
}
...
@property (nonatomic,assign) NSInteger VIDEOQUALITY;
和实施
-(void)setVideoQual:(NSInteger)quality
{
NSLog(@"SETTING VIDEO QUALITY %ld",quality);
VIDEOQUALITY=quality;
}
你会得到想要的行为。