1

デリゲートを使用して2番目のタブから最初のタブにデータを渡したいのですが、デリゲートメソッドがトリガーされていません。コードは次のとおりです(ストーリーボードを使用しています)

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

2番目のタブにスライダーを設定し、ラベルのある最初のタブにサイダー値を送信しようとしています。self.delegateはnullを出力し、delegateメソッドは呼び出されません。

4

2 に答える 2

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

そして、それはそれを行う必要があります

于 2012-06-21T08:31:54.563 に答える
0

2 番目のタブに実際にアクセスするにはどうすればよいでしょうか。viewDidLoad でインスタンスを作成していますが、タブを切り替えると、そのインスタンスは画面に表示されません。システムは、使用する別のインスタンスを作成します。これにアクセスするには、タブ バー コントローラーのviewcontrollersプロパティにアクセスし、SecondViewController をチェックします。

編集:デザインに疑問を呈することもできます。あなたのアプリについてはよくわかりませんが、最初のタブが 2 番目のタブのデリゲートになるべきではない可能性があります。データを渡したい場合は、NSNotificationCenterまたは永続ストレージの使用を検討してください。

于 2012-06-21T08:11:35.150 に答える