我一直关注Big Nerd Ranch的iOS编程指南(第3版)来设置我的Xcode项目,该项目显示了我公司产品的列表,然后是每个产品的详细视图。
我让应用程序以我需要的方式游泳,但是当我试图想象用户体验时,我开始遇到麻烦。添加一个适用于iPad的UISplitViewController让我无法忍受头疼和下午的浪费。
目前,我在代理相关代码上报告了语义问题。一个在DetailViewController.h中,另一个在ListViewController.m中。
我会在发布之前总结一下我对此代码的意图,但由于我的经验不足,我可能会遗漏一些细微之处:
AppDelegate分配UITableViewController(ListViewController类)和UIViewController(DetailViewController类),然后检查iPad。如果是iPad,它会使用两个视图的数组创建一个UISplitViewController。否则,它将ListViewController加载为主视图。
在我尝试在两个视图之间创建委托关系之前,应用程序正在成功构建,但iPad UISplitViewController仅加载了一个空的详细信息视图。 iphone加载了ListViewController,然后选择一行显示一个空的详细信息视图(DetailViewController)。当您返回到TableView,并选择相同或另一个表格单元格时,正确的信息将加载到DetailView中。这让我相信TableView的初始实例没有正确传递选择,但返回它(重新分配它?)将纠正问题。我希望代理设置能解决这个问题。由于我不能使那部分工作,我无法测试这个理论。我想我会提到它。
关于UISplitViewController的问题和教程,我已经尽可能多地了解了(正确的关键词和搜索词不适合我),但它们与我在项目中已经设置的内容有很大不同,无论是应用程序的行为还是代码的整体结构。当我看起来如此接近时,我宁愿不必重新开始。
我打开了BigNerdRanch示例代码(确实有效),正如我所说,唯一的区别似乎与我想要显示信息的方式有关。在这一点上,我需要一些帮助,请找出我做错了什么。
提前致谢!
AppDelegate.m:
#import "ProductFeedAppDelegate.h"
#import "ListViewController.h"
#import "DetailViewController.h"
@implementation ProductFeedAppDelegate
@synthesize window = _window;
@synthesize managedObjectContext = _managedObjectContext;
@synthesize managedObjectModel = _managedObjectModel;
@synthesize persistentStoreCoordinator = _persistentStoreCoordinator;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
ListViewController *lvc = [[ListViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *masterNav = [[UINavigationController alloc] initWithRootViewController:lvc];
DetailViewController *dvc = [[DetailViewController alloc] init];
[lvc setDetailViewController:dvc];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
UINavigationController *detailNav = [[UINavigationController alloc] initWithRootViewController:dvc];
NSArray *vcs = [NSArray arrayWithObjects:masterNav, detailNav, nil];
UISplitViewController *svc = [[UISplitViewController alloc] init];
//set delegate
[svc setDelegate:dvc];
[svc setViewControllers:vcs];
[[self window] setRootViewController:svc];
} else {
[[self window] setRootViewController:masterNav];
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
//... trimmed out some template code to spare you
@end
` ListViewController.h:
#import <Foundation/Foundation.h>
#import "ProductItemCell.h"
//#import "ItemStore.h"
#import "DetailViewController.h"
@class DetailViewController;
@class RSSChannel;
@interface ListViewController : UITableViewController
{
RSSChannel *channel;
}
@property (nonatomic, strong) DetailViewController *detailViewController;
-(void)fetchEntries;
@end
//A new protocol named ListViewControllerDelegate
@protocol ListViewControllerDelegate
//Classes that conform to this protocol must implement this method:
- (void)listViewController:(ListViewController *)lvc handleObject:(id)object;
@end
ListViewController.m:
#import "ListViewController.h"
#import "RSSChannel.h"
#import "RSSItem.h"
#import "DetailViewController.h"
#import "ContactViewController.h"
#import "FeedStore.h"
@implementation ListViewController
@synthesize detailViewController;
- (void)transferBarButtonToViewController:(UIViewController *)vc
{
// Trimming Code
}
- (id)initWithStyle:(UITableViewStyle)style
{
// Trimming Code
}
- (void)showInfo:(id)sender
{
// Create the contact view controller
ContactViewController *contactViewController = [[ContactViewController alloc] init];
if ([self splitViewController]) {
[self transferBarButtonToViewController:contactViewController];
UINavigationController *nvc = [[UINavigationController alloc]
initWithRootViewController:contactViewController];
// Create an array with our nav controller and this new VC's nav controller
NSArray *vcs = [NSArray arrayWithObjects:[self navigationController],
nvc,
nil];
// Grab a pointer to the split view controller
// and reset its view controllers array.
[[self splitViewController] setViewControllers:vcs];
// Make contact view controller the delegate of the split view controller
[[self splitViewController] setDelegate:contactViewController];
// If a row has been selected, deselect it so that a row
// is not selected when viewing the info
NSIndexPath *selectedRow = [[self tableView] indexPathForSelectedRow];
if (selectedRow)
[[self tableView] deselectRowAtIndexPath:selectedRow animated:YES];
} else {
[[self navigationController] pushViewController:contactViewController
animated:YES];
}
// Give the VC the channel object through the protocol message
// [channelViewController listViewController:self handleObject:channel];
}
- (void)viewDidLoad
{
// Trimming Code
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return [[channel items] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
// Trimming Code
}
- (void)fetchEntries
{
// Trimming Code
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (![self splitViewController])
[[self navigationController] pushViewController:detailViewController animated:YES];
else {
[self transferBarButtonToViewController:detailViewController];
// We have to create a new navigation controller, as the old one
// was only retained by the split view controller and is now gone
UINavigationController *nav =
[[UINavigationController alloc] initWithRootViewController:detailViewController];
NSArray *vcs = [NSArray arrayWithObjects:[self navigationController],
nav,
nil];
[[self splitViewController] setViewControllers:vcs];
// Make the detail view controller the delegate of the split view controller
[[self splitViewController] setDelegate:detailViewController];
}
RSSItem *item = [[channel items] objectAtIndex:[indexPath row]];
// Next line reports: No visible @interface for 'DetailViewController' declares the selector 'listViewController:handleObject:'
[detailViewController listViewController:self handleObject:item];
}
@end
DetailViewController.h:
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "ListViewController.h"
@class RSSItem;
@class Reachability;
@interface DetailViewController : UIViewController <ListViewControllerDelegate> // Cannot find protocol declaration for 'ListViewControllerDelegate'
{
__weak IBOutlet UILabel *nameField;
__weak IBOutlet UITextView *descriptField;
__weak IBOutlet UIImageView *imageView;
__weak IBOutlet UITextView *introtextField;
__weak IBOutlet UIButton *dsButton;
__weak IBOutlet UIButton *aeButton;
__weak IBOutlet UIButton *imButton;
}
-(BOOL)reachable;
@property (nonatomic, strong) RSSItem *item;
@property (nonatomic, strong) UIImage *productImage;
@end
DetailViewController.m:
#import "DetailViewController.h"
#import "RSSItem.h"
#import "RSSChannel.h"
#import "Reachability.h"
@interface DetailViewController ()
@end
@implementation DetailViewController
- (void)listViewController:(ListViewController *)lvc handleObject:(id)object
{
//RSSItem *item = object; //This was in the example code but if left in the next line reported "Local declaration of 'item' hides instance variable"
// Validate the RSSItem
if (![item isKindOfClass:[RSSItem class]])
return;
[self setItem:item];
[[self navigationItem] setTitle:[item name]];
[nameField setText:[item name]];
[descriptField setText:[item descript]];
[introtextField setText:[item introtext]];
}
@synthesize item;
- (BOOL)reachable{
// Trimming Code
}
- (void)viewDidLoad
{
[super viewDidLoad];
[[self view] setBackgroundColor:[UIColor whiteColor]];
}
- (void)viewWillAppear:(BOOL)animated
{
if (item){
[super viewWillAppear:animated];
[nameField setText:[item name]];
[descriptField setText:[item descript]];
[introtextField setText:[item introtext]];
// Trimming Code (all the stuff that looks for this or that value and acts upon it)
} else {
// The following appears in the log:
NSLog(@"There's no item selected");
}
}
@end
答案 0 :(得分:1)
我认为你遇到了一个问题,编译器因为有几个
而感到困惑#import "DetailViewController.h"
如果从ListViewController.h中删除此导入并保留
@class DetailViewController;
然后我认为这将摆脱你的编译器问题。
您可能需要将< UISplitViewControllerDelegate >
添加到其他几个类中。看起来您在拆分视图中将它们设置为委托但未采用协议。
答案 1 :(得分:1)
委托关系未正确设置100%。以下是修复方法。
在ListViewController.m中,添加了一个类扩展名:
@interface ListViewController() <UISplitViewControllerDelegate>
@end
在ListViewController.h中,删除了:
#import "DetailViewController.h"
在DetailViewController.h中,将行更改为:
@interface DetailViewController : UIViewController <ListViewControllerDelegate, UISplitViewControllerDelegate>
在ContactViewController.h中,将行更改为:
@interface ContactViewController : UIViewController <MFMailComposeViewControllerDelegate, UISplitViewControllerDelegate>
这些事情清除了所有错误。这不是,正如我在原始帖子中所希望的那样,纠正了我的项目没有被传递给detailViewController的问题,因为该问题是在DetailViewController.m的handleObject语句中使用“item”而不是“object”的结果。