2

在问了一些问题之后,我学会了如何将订单从一个视图控制器发送到另一个视图控制器并设法编写代码使其正常工作,但没有任何反应......

在我的项目中,我有两个名为sayfa1和的视图控制器sayfa23。当sayfa1点击一个按钮时,它会打开sayfa23并写在一个标签上(随机你好,见下面的代码)但它没有发生。在模拟器上,该按钮只打开了sayfa23标签,这就是它没有发生的事情。如果您查看代码,您可以更好地理解它。

说fa1.h

#import <UIKit/UIKit.h>

@protocol sayfa1Delegate <NSObject>

- (void)dealWithButton1;

@end


@interface sayfa1 : UIViewController

@property(nonatomic,assign) id<sayfa1Delegate> delegate;

@end

sayfa1.m

#import "sayfa1.h"

@interface sayfa1 ()

@end

@implementation sayfa1

@synthesize delegate;

-(IBAction)button
{
    [delegate dealWithButton1];
}

@end

sayfa23.h

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

@interface sayfa23 : UIViewController <sayfa1Delegate> 
{
    IBOutlet UILabel *label;
    sayfa1 *vc1 ;
}

@end

sayfa23.m

#import "sayfa23.h"
#import "sayfa1.h"

@interface sayfa23 ()

@end

@implementation sayfa23

- (void)dealWithButton1
{
    vc1.delegate = self;    
    int random_num;
    random_num = (arc4random() % 5 - 1) + 1;
    if (random_num == 1)
    { 
        label.text = @"hello1";    
    }
    else if (random_num == 2)
        label.text = @"hello2";
    else if (random_num == 3)
        label.text = @"hello3";
    else if (random_num == 4)
        label.text = @"hello4";
}

@end

编写此代码后,我连接了按钮,sayfa23因此它将打开新页面,我还连接了该按钮sayfa1以接收按钮操作,并连接了标签 (on sayfa23) 以sayfa23接收标签订单。但正如我所说的,没有发生任何错误,也没有你好,我做错了什么?我导入sayfa1.hsayfa23.h在我的一些 h 文件的顶部导致 Xcode 给我一个关于未定义并解决该问题的错误,但这是我的错误还是其他原因。

我想要的例子。

  • 用户打开应用

  • sayfa1显示在屏幕上

  • 用户单击按钮并sayfa23显示标签文本sayfa23由按钮更改,该按钮在sayfa1其上写入随机 hello1..2..3 等...

我做错了什么?

4

3 回答 3

2

重读你的问题,你问你的第一个视图控制器如何打开第二个视图控制器并设置一个文本框。如果那确实是您要尝试做的事情,那将是一个简单得多的问题,根本不需要委托协议或委托。

前两个答案是通过代表的讨论得出的,但这是为了解决不同的问题。仅当您需要第二个控制器将某些内容传递回第一个控制器时,才需要委托。但是如果你只是想让你的第二个控制器从第一个控制器接收一些东西,它很简单:

//  FirstViewController.h

#import <UIKit/UIKit.h>

@interface FirstViewController : UIViewController

@end

使用如下实现:

//  FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation FirstViewController

- (NSString *)generateRandomText
{
    NSString *result;

    int random_num;
    random_num = (arc4random() % 5 - 1) + 1;
    if (random_num == 1)
        result = @"hello1";    
    else if (random_num == 2)
        result = @"hello2";
    else if (random_num == 3)
        result = @"hello3";
    else if (random_num == 4)
        result = @"hello4";

    return result;
}

// if you're using NIBs, it might be something like...
// you only need this method if you're using NIBs and you've manually hooked a button up to this
// if you're using segues, get rid of `goToNextViewController` and just use the following `prepareForSegue

- (IBAction)goToNextViewController:(id)sender
{
    SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
    secondController.textFromParent = [self generateRandomText];
    [self.navigationController pushViewController:secondController animated:YES];
}

// if you're using segues, give your segue an identifier, e.g. toSecondViewSegue, in Interface Builder and reference the exact same identifier here
// if you're not using segues, you don't need this prepareForSegue method

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"toSecondViewSegue"])
    {
        SecondViewController *destinationController = segue.destinationViewController;

        destinationController.textFromParent = [self generateRandomText];
    }
}

@end

您的第二个控制器可能如下所示:

//  SecondViewController.h

#import <UIKit/UIKit.h>

@interface SecondViewController : UIViewController

@property (strong, nonatomic) NSString *textFromParent;
@property (weak, nonatomic) IBOutlet UILabel *label;

@end

使用如下实现:

//  SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController

@synthesize textFromParent = _textFromParent;
@synthesize label = _label;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.label.text = self.textFromParent;
}

@end
于 2012-07-24T17:25:56.610 回答
1

您的第一个控制器应该在实例化第二个控制器时,将第二个的委托设置为指向第一个视图控制器。因此,您的第一个视图控制器可能如下所示:

//  FirstViewController.h

#import <UIKit/UIKit.h>

@protocol FirstViewControllerDelegate <NSObject>

- (void)dealWithButton;

@end

@interface FirstViewController : UIViewController <FirstViewControllerDelegate>

@end

使用如下实现:

//  FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@implementation FirstViewController

- (IBAction)goToNextViewController:(id)sender
{
    SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
    secondController.delegate = self;
    [self.navigationController pushViewController:secondController animated:YES];
}

- (void)dealWithButton
{
    NSLog(@"Dealt with button from second controller");
}

@end

您的第二个控制器可能如下所示:

//  SecondViewController.h

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

@class FirstViewController;

@interface SecondViewController : UIViewController

@property (weak, nonatomic) id<FirstViewControllerDelegate> delegate;
@property (weak, nonatomic) IBOutlet UILabel *label;

- (IBAction)buttonPressed:(id)sender;

@end

使用如下实现:

//  SecondViewController.m

#import "SecondViewController.h"

@implementation SecondViewController

@synthesize delegate = _delegate;
@synthesize label = _label;

- (IBAction)buttonPressed:(id)sender
{
    int random_num;
    random_num = (arc4random() % 5 - 1) + 1;
    if (random_num == 1)
        self.label.text = @"hello1";    
    else if (random_num == 2)
        self.label.text = @"hello2";
    else if (random_num == 3)
        self.label.text = @"hello3";
    else if (random_num == 4)
        self.label.text = @"hello4";

    [self.delegate dealWithButton];
}
@end

更新:

您最初的问题没有明确说明您是否希望标签位于第一个控制器或第二个控制器上。我上面的回答假设您希望在第二个控制器上使用它,但回想起来,您可能希望在第一个控制器(委托)上使用它。如果是这样,下面的代码就是这样做的。请注意,我不只是在 中更新第一个视图控制器的标签dealWithButton,因为这很危险,因为您不知道视图是否可见(如果您收到 ,则可能已卸载didReceiveMemoryWarning)。所以我等待viewWillAppear。同样,第一个视图控制器:

//  FirstViewController.h

#import <UIKit/UIKit.h>

@protocol FirstViewControllerDelegate <NSObject>

- (void)dealWithButton;

@end

@interface FirstViewController : UIViewController <FirstViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UILabel *label;

@end

及其实现:

//  FirstViewController.m

#import "FirstViewController.h"
#import "SecondViewController.h"

@interface FirstViewController ()
{
    NSString *_labelText;
}
@end

@implementation FirstViewController

@synthesize label = _label;

// if you're using storyboards, it would be like:

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"delegateSegue"])
    {
        SecondViewController *destinationController = segue.destinationViewController;
        FirstViewController *sourceController = segue.sourceViewController;

        destinationController.delegate = sourceController;
    }
}

// if not using storyboards, you probably have a button like:

- (IBAction)goToNextViewController:(id)sender
{
    SecondViewController *secondController = [[SecondViewController alloc] initWithNibName:@"SecondView" bundle:nil];
    secondController.delegate = self;
    [self.navigationController pushViewController:secondController animated:YES];
}

- (void)dealWithButton
{
    // note, because this is being called by the second view controller, you should *not* update the UI
    // directly, because you can't be assured this view controller's view is still in memory (if you got
    // a didReceiveMemoryWarning while on the second view controller, this first view controller will 
    // stay in memory, but its view could have been released). So save what you want the label to be,
    // and update it on viewWillAppear (and if the view was released, it will be reloaded by the time
    // you hit viewWillAppear.
    //
    // clearly, if you were doing view controller containment and this was the parent view, you wouldn't
    // want to do this. But I assume you're dealing with a simple push/present view controller situation.

    int random_num;
    random_num = (arc4random() % 5 - 1) + 1;
    if (random_num == 1)
        _labelText = @"hello1";    
    else if (random_num == 2)
        _labelText = @"hello2";
    else if (random_num == 3)
        _labelText = @"hello3";
    else if (random_num == 4)
        _labelText = @"hello4";

    NSLog(@"Dealt with button from second controller");
}

- (void)viewWillAppear:(BOOL)animated
{
    self.label.text = _labelText;
}

@end

第二个视图控制器:

//  SecondViewController.h

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

@class FirstViewController;

@interface SecondViewController : UIViewController

@property (weak, nonatomic) id<FirstViewControllerDelegate> delegate;

- (IBAction)buttonPressed:(id)sender;

@end

及其实现:

//  SecondViewController.m

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

@synthesize delegate = _delegate;

- (IBAction)buttonPressed:(id)sender
{
    [self.delegate dealWithButton];
}
@end
于 2012-07-24T14:25:08.287 回答
0

尝试将以下方法添加到您的 sayfa23 实现中:

- (void)viewDidLoad
{
    vc1 = [[sayfa1 alloc] init];
    vc1.delegate = self;
}

并删除 vc1.delegate = self; 从你的 dealWithButton1 方法。

编辑:您必须了解该方法 dealWithButton1 永远不会被调用,因为您永远不会将消息发送到对象。因此,您永远不会设置 vc1 的委托。最好使用 viewDidLoad 方法进行一些设置,该方法在视图加载时调用。在那里您可以分配初始化(创建一个实例)sayfa1 类,并将其分配给您的属性 vc1。分配对象后,您可以向它发送消息。然后您可以设置委托。

sayfa23.h

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

@interface sayfa23 : UIViewController <sayfa1Delegate> 



{

    IBOutlet UILabel *label;
}
    @property (nonatomic, strong) sayfa1 *vc1 ;



@end

sayfa23.m

#import "sayfa23.h"
#import "sayfa1.h"

@interface sayfa23 ()

@end

@implementation sayfa23
@synthesize vc1;


- (void)viewDidLoad
{
    vc1 = [[sayfa1 alloc] init];
    vc1.delegate = self;
}

- (void)dealWithButton1

{
    int random_num;
    random_num = (arc4random() % 5 - 1) + 1;
    if (random_num == 1)
    { 
        label.text = @"hello1";    
    }
    else if (random_num == 2)
        label.text = @"hello2";
    else if (random_num == 3)
        label.text = @"hello3";
    else if (random_num == 4)
        label.text = @"hello4";
}


@end
于 2012-07-24T13:58:18.333 回答