0
我有2個意見。 firstView
和secondView
。 firstView
需要secondView
的收藏夾陣列,所以我嘗試調用協議中定義的getFavourites
方法。然而,這返回null,這對我來說似乎很陌生,因爲一切都合情合理。iPhone代表問題
這裏是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
有誰知道這是爲什麼不工作的任何想法?
你檢查委託當時getFavorites分配被稱爲? – Fuggly
你是什麼意思?我需要將其分配給什麼以及如何分配? – JheeBz
用'@property(nonatomic,retain)IBOutlet SecondViewController * delegate'替換委託屬性,轉到xib,放下視圖控制器圖標,將其設置爲SecondController並將其鏈接到屬性。另一種方法是在viewDidLoad中實例化控制器。我認爲你不太理解委託和控制器的概念,但這並不妨礙你的工作。 – Jano