我看到另一个问题和研究, 但无法传递数据。
这是我的代码。
@protocol homeViewControllerDelegate <NSObject>
-(void)addItemViewController:(Float32)item;
@end
@interface HomeViewController:UIViewController @property(nonatomic,retain)id delegate;
#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;
}
@interface GraphViewController : UIViewController<homeViewControllerDelegate>
-(void)addItemViewController:(Float32)item;
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的图形,但是这段代码没有给出错误但也没有用。
我不知道问题。
你能帮助我吗?
答案 0 :(得分:0)
为了确保您理解我的评论,我给您写了一个示例代表流程的演示应用。
#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;
}
...
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
@interface GraphViewController : UIViewController <HomeViewControllerDelegate>
@property (nonatomic, strong) HomeViewController *homeVC;
@property (nonatomic, assign) Float32 data;
@end
#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
#import <UIKit/UIKit.h>
@protocol HomeViewControllerDelegate <NSObject>
-(void)addItem:(Float32)item;
@end
@interface HomeViewController : UIViewController
@property (nonatomic, weak) id<HomeViewControllerDelegate> delegate;
@end
#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