【问题标题】:Passing data between Tabs using delegate not working使用委托在选项卡之间传递数据不起作用
【发布时间】:2012-06-21 16:31:54
【问题描述】:

我想使用委托将数据从第二个选项卡传递到第一个选项卡。但委托方法未触发。这是代码(我正在使用情节提要)

在 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

在 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

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

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

@end

在 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

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

【问题讨论】:

    标签: iphone objective-c ios delegates tabbar


    【解决方案1】:

    很简单,因为您已经有了委托代码,只需更改创建SecondViewController 对象的行。而不是创建一个新的,你应该有一个标签栏将显示的引用。

    在FirstViewController的viewDidLoad中, 改变

    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]; 
    

    应该这样做

    【讨论】:

      【解决方案2】:

      您实际上是如何进入第二个标签的?您正在 viewDidLoad 中创建它的一个实例,但如果您切换选项卡,该实例将不会用于在屏幕上显示。系统将创建另一个它将使用的实例。您可以通过访问标签栏控制器的 viewcontrollers 属性并检查 SecondViewController 来访问它。

      编辑:你也可以质疑你的设计。我对您的应用了解不多,但您的第一个选项卡可能不应该真正成为您的第二个选项卡的代表。如果您想传递数据,请考虑使用NSNotificationCenter 或持久存储。

      【讨论】:

        猜你喜欢
        • 2013-05-16
        • 1970-01-01
        • 2013-07-31
        • 1970-01-01
        • 2016-12-07
        • 1970-01-01
        • 1970-01-01
        • 2012-12-17
        • 1970-01-01
        相关资源
        最近更新 更多