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

时间:2012-06-21 08:04:24

标签: iphone objective-c ios delegates tabbar

我想使用delegates将数据从第二个标签传递到第一个标签。但是没有触发委托方法。这是代码(我使用的是故事板)

在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

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

2 个答案:

答案 0 :(得分: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]; 

那应该这样做

答案 1 :(得分:0)

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

编辑:您也可以质疑您的设计。我对您的应用程序了解不多,但您的第一个选项卡可能不是第二个选项卡的委托。如果要传递数据,请考虑使用NSNotificationCenter或持久存储。