【发布时间】:2012-02-07 00:37:26
【问题描述】:
我正在使用 Storyboards 学习 Xcode 4.2,并尝试制作一个通用应用程序。
在 iPhone 上一切正常,但在 iPad 端,将 3 个文本字段(1 个 TextField,2 个 TextViews)分配给拆分视图控制器的详细视图时存在问题。
我使用了 Storyboard 中的新功能,我将详细视图中的新字段链接到详细信息头文件,它会自动创建属性和综合语句(非常好的功能)。
但是当我想要为详细信息字段分配值时(方法drinkChanged),DetailViewController 视图中没有显示任何内容。我验证了字典值在分配时就在那里。
有趣的是,当调用 viewWillAppear 时调用方法 refreshView 时,它会起作用。但是如果drinkChanged调用了refreshView方法(意味着用户在MasterViewController表中选择了一行),那么它就不起作用了。
有人知道我在哪里可能会错过公共汽车吗?
这是我的代码:
MasterViewController.h
#import <UIKit/UIKit.h>
@interface MasterViewController : UITableViewController {
NSMutableArray *drinks;
}
@property (nonatomic, retain) NSMutableArray *drinks;
@end
MasterViewController.m
#import "MasterViewController.h"
#import "DetailViewController.h"
#import "AddDrinkViewController.h"
#import "DrinkConstants.h"
@implementation MasterViewController
@synthesize drinks;
.
.
.
- (void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (!self.editing) {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
NSLog(@"didSelectRowAtIndexPath...iPad");
// Pass the selected object to the drinkChanged method in DetailViewController.m.
DetailViewController *splitViewDetailView = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
[splitViewDetailView drinkChanged:[self.drinks objectAtIndex:indexPath.row]];
}
else {
NSLog(@"didSelectRowAtIndexPath...iPhone");
DetailViewController *detailViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"Detail"];
detailViewController.drink = [self.drinks objectAtIndex:indexPath.row];
// Pass the selected object to the new view controller.
[self.navigationController pushViewController:detailViewController animated:YES];
}
}
else {
AddDrinkViewController *editingDrinkVC = [self.storyboard instantiateViewControllerWithIdentifier:@"AddDrink"];
editingDrinkVC.drink = [self.drinks objectAtIndex:indexPath.row];
editingDrinkVC.drinkArray = self.drinks;
[self.navigationController presentModalViewController:editingDrinkVC animated:YES];
}
}
DetailViewController.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController {
NSDictionary *drink;
}
@property (strong, nonatomic) id detailItem;
@property (nonatomic, retain) NSDictionary *drink;
@property (strong, nonatomic) IBOutlet UITextField *nameTextField;
@property (strong, nonatomic) IBOutlet UITextView *ingredientsTextView;
@property (strong, nonatomic) IBOutlet UITextView *directionsTextView;
- (void) refreshView;
- (void) drinkChanged:(NSDictionary *) newDrink;
@end
DetailViewController.m
#import "DetailViewController.h"
#import "DrinkConstants.h"
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController
@synthesize detailItem;
@synthesize drink;
@synthesize nameTextField;
@synthesize ingredientsTextView;
@synthesize directionsTextView;
.
.
.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSLog(@"viewWillAppear");
[self refreshView];
}
- (void) refreshView {
NSLog(@"refreshView");
// Set up our UI with the provided drink.
self.nameTextField.text = [self.drink objectForKey:NAME_KEY];
self.ingredientsTextView.text = [self.drink objectForKey:INGREDIENTS_KEY];
self.directionsTextView.text = [self.drink objectForKey:DIRECTIONS_KEY];
NSLog(@"NAME_KEY = %@", [self.drink objectForKey:NAME_KEY]);
NSLog(@"INGREDIENTS_KEY = %@", [self.drink objectForKey:INGREDIENTS_KEY]);
NSLog(@"DIRECTIONS_KEY = %@", [self.drink objectForKey:DIRECTIONS_KEY]);
NSLog(@"nameTextField = %@", self.nameTextField.text);
NSLog(@"ingredientsTextView = %@", self.ingredientsTextView.text);
NSLog(@"directionsTextView = %@", self.directionsTextView.text);
}
- (void) drinkChanged:(NSDictionary *) newDrink {
NSLog(@"drinkChanged");
self.drink = newDrink;
[self refreshView];
}
NSLog 输出
GNU gdb 6.3.50-20050815 (Apple version gdb-1708) (Mon Aug 8 20:32:45 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".sharedlibrary apply-load-rules all
Attaching to process 1187.
2012-01-31 13:10:26.061 DrinkMixer[1187:f803] viewDidLoad
2012-01-31 13:10:26.087 DrinkMixer[1187:f803] viewWillAppear
2012-01-31 13:10:26.088 DrinkMixer[1187:f803] refreshView
2012-01-31 13:10:26.089 DrinkMixer[1187:f803] NAME_KEY = (null)
2012-01-31 13:10:26.090 DrinkMixer[1187:f803] INGREDIENTS_KEY = (null)
2012-01-31 13:10:26.090 DrinkMixer[1187:f803] DIRECTIONS_KEY = (null)
2012-01-31 13:10:26.091 DrinkMixer[1187:f803] nameTextField = (null)
2012-01-31 13:10:26.091 DrinkMixer[1187:f803] ingredientsTextView =
2012-01-31 13:10:26.092 DrinkMixer[1187:f803] directionsTextView =
2012-01-31 13:10:32.878 DrinkMixer[1187:f803] didSelectRowAtIndexPath...iPad
2012-01-31 13:10:32.879 DrinkMixer[1187:f803] drinkChanged
2012-01-31 13:10:32.879 DrinkMixer[1187:f803] refreshView
2012-01-31 13:10:32.880 DrinkMixer[1187:f803] NAME_KEY = Blue Dog
2012-01-31 13:10:32.880 DrinkMixer[1187:f803] INGREDIENTS_KEY = vodka,grapefruit juice
2012-01-31 13:10:32.881 DrinkMixer[1187:f803] DIRECTIONS_KEY = Combine both while cold.
2012-01-31 13:10:32.881 DrinkMixer[1187:f803] nameTextField = (null)
2012-01-31 13:10:32.882 DrinkMixer[1187:f803] ingredientsTextView = (null)
2012-01-31 13:10:32.882 DrinkMixer[1187:f803] directionsTextView = (null)
NSLog 中的最后 3 行是我希望从字典键中获取值的地方,但它们却返回 (null)。
【问题讨论】:
标签: ios4 xcode4.2 storyboard