无法将数据传递给控制器;为什么它不起作用?
Can't pass data to controller; why doesn't it work?
我看到另一个问题并研究了,
但无法传递数据。
这是我的代码。
在HomeViewController.h
@protocol homeViewControllerDelegate <NSObject>
-(void)addItemViewController:(Float32)item;
@end
@interface HomeViewController : UIViewController
@属性(非原子,保留)id 委托;
在HomeViewController.mm
#import "HomeViewController.h"
static HomeViewController *mainController = nil;
#pragma mark MAIN CALLBACK
void monoAudioCallback( Float32 * buffer, UInt32 frameSize, void * userData )
{
dispatch_async(dispatch_get_main_queue(), ^{
[homeOsciView setDataMaxValue:fftMaxVal minValue:0];
[homeOsciView addAndDrawData:fftData lenght:length];
if(fftMaxVal >=0){
[mainController.delegate addItemViewController:fftMaxVal];
}
[homeOsciView addAndDrawUILabelHz: HZ];
});
}
viewDidLoad{
mainController = self;
}
在graphViewController.h
@interface GraphViewController : UIViewController<homeViewControllerDelegate>
-(void)addItemViewController:(Float32)item;
在graphViewController.m
viewDidLoad(){
homevc = [[HomeViewController alloc]init];
homevc.delegate = self;
}
-(void)addItemViewController:(Float32)item{
self.testdata = item;
printf("thisis graphView data : %f\n",self.testdata);
}
我想绘制 HomeViewController 将数据传递给 graphViewController 的图形,但是这段代码没有报错,但也不起作用。
我不知道问题。
你能帮帮我吗?
为了确保您理解我的意见,我为您编写了一个示例委托流程的演示应用程序。
AppDelegate.m
#import "GraphViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[GraphViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
...
GraphViewController.h
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
@interface GraphViewController : UIViewController <HomeViewControllerDelegate>
@property (nonatomic, strong) HomeViewController *homeVC;
@property (nonatomic, assign) Float32 data;
@end
GraphViewController.m
#import "GraphViewController.h"
@interface GraphViewController ()
@end
@implementation GraphViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor purpleColor];
self.homeVC = [[HomeViewController alloc] init];
self.homeVC.delegate = self;
NSLog(@"presenting homeVC in 2 seconds...");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self presentViewController:self.homeVC animated:YES completion:^{
NSLog(@"homeVC presented, you should get data in 3 seconds...");
}];
});
}
-(void)addItem:(Float32)item
{
self.data = item;
NSLog(@"Graph view data: %f", self.data);
}
@end
HomeViewController.h
#import <UIKit/UIKit.h>
@protocol HomeViewControllerDelegate <NSObject>
-(void)addItem:(Float32)item;
@end
@interface HomeViewController : UIViewController
@property (nonatomic, weak) id<HomeViewControllerDelegate> delegate;
@end
HomeViewController.m
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
// --------------------------------------------------------
// simulate a callback by executing code after 3 seconds
// --------------------------------------------------------
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// good thing comes in pairs :D
[self.delegate addItem:2.0];
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我在控制台输出中得到了这个:
2015-08-28 16:20:53.021 AudioCallbackDemo[6973:194320] presenting homeVC in 2 seconds...
2015-08-28 16:20:55.534 AudioCallbackDemo[6973:194320] homeVC presented, you should get data in 3 seconds...
2015-08-28 16:20:58.044 AudioCallbackDemo[6973:194320] Graph view data: 2.000000
我看到另一个问题并研究了, 但无法传递数据。
这是我的代码。
在HomeViewController.h
@protocol homeViewControllerDelegate <NSObject> -(void)addItemViewController:(Float32)item; @end
@interface HomeViewController : UIViewController @属性(非原子,保留)id 委托;
在HomeViewController.mm
#import "HomeViewController.h" static HomeViewController *mainController = nil; #pragma mark MAIN CALLBACK void monoAudioCallback( Float32 * buffer, UInt32 frameSize, void * userData ) { dispatch_async(dispatch_get_main_queue(), ^{ [homeOsciView setDataMaxValue:fftMaxVal minValue:0]; [homeOsciView addAndDrawData:fftData lenght:length]; if(fftMaxVal >=0){ [mainController.delegate addItemViewController:fftMaxVal]; } [homeOsciView addAndDrawUILabelHz: HZ]; }); } viewDidLoad{ mainController = self; }
在graphViewController.h
@interface GraphViewController : UIViewController<homeViewControllerDelegate> -(void)addItemViewController:(Float32)item;
在graphViewController.m
viewDidLoad(){ homevc = [[HomeViewController alloc]init]; homevc.delegate = self; } -(void)addItemViewController:(Float32)item{ self.testdata = item; printf("thisis graphView data : %f\n",self.testdata); }
我想绘制 HomeViewController 将数据传递给 graphViewController 的图形,但是这段代码没有报错,但也不起作用。
我不知道问题。
你能帮帮我吗?
为了确保您理解我的意见,我为您编写了一个示例委托流程的演示应用程序。
AppDelegate.m
#import "GraphViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.window.rootViewController = [[GraphViewController alloc] init];
[self.window makeKeyAndVisible];
return YES;
}
...
GraphViewController.h
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
@interface GraphViewController : UIViewController <HomeViewControllerDelegate>
@property (nonatomic, strong) HomeViewController *homeVC;
@property (nonatomic, assign) Float32 data;
@end
GraphViewController.m
#import "GraphViewController.h"
@interface GraphViewController ()
@end
@implementation GraphViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor purpleColor];
self.homeVC = [[HomeViewController alloc] init];
self.homeVC.delegate = self;
NSLog(@"presenting homeVC in 2 seconds...");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self presentViewController:self.homeVC animated:YES completion:^{
NSLog(@"homeVC presented, you should get data in 3 seconds...");
}];
});
}
-(void)addItem:(Float32)item
{
self.data = item;
NSLog(@"Graph view data: %f", self.data);
}
@end
HomeViewController.h
#import <UIKit/UIKit.h>
@protocol HomeViewControllerDelegate <NSObject>
-(void)addItem:(Float32)item;
@end
@interface HomeViewController : UIViewController
@property (nonatomic, weak) id<HomeViewControllerDelegate> delegate;
@end
HomeViewController.m
#import "HomeViewController.h"
@interface HomeViewController ()
@end
@implementation HomeViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
// --------------------------------------------------------
// simulate a callback by executing code after 3 seconds
// --------------------------------------------------------
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// good thing comes in pairs :D
[self.delegate addItem:2.0];
});
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
我在控制台输出中得到了这个:
2015-08-28 16:20:53.021 AudioCallbackDemo[6973:194320] presenting homeVC in 2 seconds...
2015-08-28 16:20:55.534 AudioCallbackDemo[6973:194320] homeVC presented, you should get data in 3 seconds...
2015-08-28 16:20:58.044 AudioCallbackDemo[6973:194320] Graph view data: 2.000000