简体   繁体   English

使用委托在选项卡之间传递数据不起作用

[英]Passing data between Tabs using delegate not working

I want to pass data from second tab to first tab using delegates.But delegate method not triggered.here is the code (I m using storyboard) 我想使用委托将数据从第二个选项卡传递到第一个选项卡,但是委托方法未触发。这是代码(我使用情节提要)

in SecondViewController.h 在SecondViewController.h中

#import <UIKit/UIKit.h>

 @class SecondViewController;
 @protocol SecondViewControllerDelegate <NSObject>

 -(void) sliderValueDidChanged: (SecondViewController *)controller data:(float)      sliderValue;

 @end

 @interface SecondViewController : UIViewController{
__weak id<SecondViewControllerDelegate> delegate;   
 }
 - (IBAction)sliderPressed:(id)sender;

 @property (weak, nonatomic) IBOutlet UISlider *mySlider;
 @property(weak,nonatomic) id<SecondViewControllerDelegate> delegate;

 @end

in SecondViewController.m 在SecondViewController.m中

#import "SecondViewController.h"


@interface SecondViewController ()

@end

 @implementation SecondViewController
 @synthesize mySlider;
 @synthesize delegate;

 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
 {
  self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}

- (void)viewDidLoad
 {
[super viewDidLoad];
// Do any additional setup after loading the view.
 }

 - (void)viewDidUnload
 {
[self setMySlider:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
 }

 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
 {
return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }

- (IBAction)sliderPressed:(id)sender {
float value = mySlider.value;

[self.delegate sliderValueDidChanged:self data:value];
NSLog(@"%@",self.delegate);
}
@end

FirstViewController.h FirstViewController.h

   #import <UIKit/UIKit.h>
#import "SecondViewController.h"

@interface FirstViewController : UIViewController<SecondViewControllerDelegate>
@property (weak, nonatomic) IBOutlet UILabel *myLabel;

@end

in FirstViewController.h 在FirstViewController.h中

#import "FirstViewController.h"


@interface FirstViewController ()

@end

@implementation FirstViewController
@synthesize myLabel;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   SecondViewController *svc = [[SecondViewController alloc]init];
   svc.delegate = self;




}

- (void)viewDidUnload
{
    [self setMyLabel:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) sliderValueDidChanged: (SecondViewController *)controller data:(float) sliderValue{
    myLabel.text = [[NSString alloc]initWithFormat:@"%f",sliderValue];
    NSLog(@"delegate called");
}

@end

I am trying to set a slider in second tab and send the sider value to first tab which has a label. 我试图在第二个选项卡中设置一个滑块,并将sider值发送到具有标签的第一个选项卡。 self.delegate printing null and delegate method never called. self.delegate打印null和从未调用过的委托方法。

Simply enough, as you already have the delegation code, only change the line where you create a SecondViewController object. 足够简单,因为您已经有了委派代码,只需更改创建SecondViewController对象的行。 Rather than creating a new one, you should have a reference to the one that the tab bar will show. 您应该参考选项卡栏将显示的内容,而不是创建一个新的内容。

In the viewDidLoad of FirstViewController, Change 在FirstViewController的viewDidLoad中,更改

SecondViewController *svc = [[SecondViewController alloc]init];

to

//assuming you have 2 view controllers in the tab bar controller, first being FirstViewController
SecondViewController *svc = [self.tabBarController.viewControllers objectAtIndex:1]; 

and that should do it 那应该做到的

How do you actually get to the second tab? 您实际上如何进入第二个标签? You're creating an instance of it in viewDidLoad, but if you switch tabs, that instance won't be used to show on screen. 您正在viewDidLoad中创建它的实例,但是如果切换选项卡,该实例将不会用于显示在屏幕上。 The system will create another instance which it'll use. 系统将创建另一个将要使用的实例。 You can access this by accessing the tab bar controller's viewcontrollers property and checking for SecondViewController. 您可以通过访问选项卡栏控制器的viewcontrollers属性并检查SecondViewController来访问它。

EDIT: You could also question your design. 编辑:您还可以质疑您的设计。 I don't know much about your app, but chances are your first tab shouldn't really be the delegate for your second tab. 我对您的应用程序了解不多,但是您的第一个选项卡实际上不应成为第二个选项卡的委托。 If you want to pass along data, consider using NSNotificationCenter or persistent storage. 如果要传递数据,请考虑使用NSNotificationCenter或永久存储。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM