在标签之间传递数据,使用委托不工作 [英] Passing data between Tabs using delegate not working

查看:102
本文介绍了在标签之间传递数据,使用委托不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用delegates将数据从第二个选项卡传递到第一个选项卡。但是委托方法没有被触发。在SecondViewController中的代码(我使用故事板)

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)

.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

in 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

在FirstViewController.h

in 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

我正在设置一个滑块在第二个选项卡中,并将sider值发送到具有标签的第一个选项卡。 self.delegate打印null和委托方法从未调用。

I am trying to set a slider in second tab and send the sider value to first tab which has a label. self.delegate printing null and delegate method never called.

推荐答案

只要您已经拥有代码代码,只需更改创建 SecondViewController 对象。而不是创建一个新的,您应该参考选项卡栏将显示的。

Simply enough, as you already have the delegation code, only change the line where you create a SecondViewController object. Rather than creating a new one, you should have a reference to the one that the tab bar will show.

在FirstViewController的viewDidLoad中,
更改

In the viewDidLoad of FirstViewController, Change

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

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

应该这样做

这篇关于在标签之间传递数据,使用委托不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆