iPhone 委托错误

发布于 2024-12-05 01:14:23 字数 2047 浏览 0 评论 0原文

我有 2 个观点。 firstViewsecondViewfirstView 需要来自 secondView 的 favorites 数组,因此我尝试调用协议中定义的 getFavourites 方法。然而,这返回 null,这对我来说似乎很奇怪,因为一切都是有道理的。

这是 firstViewController.h

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

@protocol firstViewControllerDelegate;

@interface FirstViewController : UIViewController
{
    id <firstViewControllerDelegate> delegate;
    NSMutableArray *labels;

    UIButton *button1;
    UIButton *button2;
    UIButton *button3;
    UIButton *button4;
}

- (IBAction) buttonClick: (id) sender;

@property (nonatomic, assign) id <firstViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UIButton *button2;
@property (nonatomic, retain) IBOutlet UIButton *button3;
@property (nonatomic, retain) IBOutlet UIButton *button4;

@end

@protocol firstViewControllerDelegate
- (NSMutableArray *) getFavourites;
- (void) setFavourites: NSMutableArray;
@end

firstViewController.m

@synthesize delegate;

.......

- (void) viewWillAppear:(BOOL)animated
{
    NSMutableArray *favourites = [delegate getFavourites]; // favourites is empty after this line
    [button1 setTitle:[[favourites objectAtIndex:1] name] forState:UIControlStateNormal];
    NSLog(@"VIEW APPEARED. BUTTON TITLE IS: %@", button1.currentTitle);
}

SecondViewController.h

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

@interface SecondViewController: UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, firstViewControllerDelegate>
{
        NSMutableArray *favourites;
        NSMutableArray *drinks;
}

@property (nonatomic, retain) NSMutableArray *drinks;
@property (nonatomic, retain, getter = getFavourites) NSMutableArray *favourites;

- (void) addDrink: (NSString *) name;

@end

有人知道为什么这不起作用吗?

I've got 2 views. The firstView and the secondView. The firstView needs the favourites array from the secondView, so I try to call the getFavourites method defined in the protocol. This returns null however, which seems strange to me because everything makes sense.

Here is the firstViewController.h:

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

@protocol firstViewControllerDelegate;

@interface FirstViewController : UIViewController
{
    id <firstViewControllerDelegate> delegate;
    NSMutableArray *labels;

    UIButton *button1;
    UIButton *button2;
    UIButton *button3;
    UIButton *button4;
}

- (IBAction) buttonClick: (id) sender;

@property (nonatomic, assign) id <firstViewControllerDelegate> delegate;
@property (nonatomic, retain) IBOutlet UIButton *button1;
@property (nonatomic, retain) IBOutlet UIButton *button2;
@property (nonatomic, retain) IBOutlet UIButton *button3;
@property (nonatomic, retain) IBOutlet UIButton *button4;

@end

@protocol firstViewControllerDelegate
- (NSMutableArray *) getFavourites;
- (void) setFavourites: NSMutableArray;
@end

firstViewController.m

@synthesize delegate;

.......

- (void) viewWillAppear:(BOOL)animated
{
    NSMutableArray *favourites = [delegate getFavourites]; // favourites is empty after this line
    [button1 setTitle:[[favourites objectAtIndex:1] name] forState:UIControlStateNormal];
    NSLog(@"VIEW APPEARED. BUTTON TITLE IS: %@", button1.currentTitle);
}

SecondViewController.h

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

@interface SecondViewController: UIViewController <UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate, firstViewControllerDelegate>
{
        NSMutableArray *favourites;
        NSMutableArray *drinks;
}

@property (nonatomic, retain) NSMutableArray *drinks;
@property (nonatomic, retain, getter = getFavourites) NSMutableArray *favourites;

- (void) addDrink: (NSString *) name;

@end

Does anybody have any idea why this isn't working?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十秒萌定你 2024-12-12 01:14:23

您确定要设置 FirstViewController 的委托属性吗?即:

FirstViewController *firstController = // Instantiate the controller;
firstController.delegate = secondController;
[self.navigationController pushViewController:firstController animated:YES];

secondController 应该在实例化之前存在,否则您将设置一个 nil

Are you sure you're setting the delegate property of FirstViewController? i.e.:

FirstViewController *firstController = // Instantiate the controller;
firstController.delegate = secondController;
[self.navigationController pushViewController:firstController animated:YES];

secondController should exist before instantiation, otherwise you're setting a nil value

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文