无法将数据传递给控制器​​;为什么不起作用?

时间:2015-08-28 07:03:36

标签: ios objective-c viewcontroller

我看到另一个问题和研究, 但无法传递数据。

这是我的代码。

    HomeViewController.h中的
  1. @protocol homeViewControllerDelegate <NSObject>
    -(void)addItemViewController:(Float32)item;
    @end
    

    @interface HomeViewController:UIViewController @property(nonatomic,retain)id delegate;

  2. HomeViewController.mm中的
  3. #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;
     }
    
  4. graphViewController.h中的
  5. @interface GraphViewController : UIViewController<homeViewControllerDelegate>
    -(void)addItemViewController:(Float32)item;
    
  6. graphViewController.m中的
  7.     viewDidLoad(){
        homevc = [[HomeViewController alloc]init];
                homevc.delegate = self;
    }
    
        -(void)addItemViewController:(Float32)item{
            self.testdata = item;
            printf("thisis graphView data : %f\n",self.testdata);
        }
    
  8. 我想绘制HomeViewController将数据传递给graphViewController的图形,但是这段代码没有给出错误但也没有用。

    我不知道问题。

    你能帮助我吗?

1 个答案:

答案 0 :(得分:0)

为了确保您理解我的评论,我给您写了一个示例代表流程的演示应用。

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