gpt4 book ai didi

ios - UISplitView,在设置委托(delegate)时接收语义问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:18:01 27 4
gpt4 key购买 nike

我一直在按照 Big Nerd Ranch 的 iOS 编程指南(第 3 版)设置我的 Xcode 项目,该项目显示我公司产品的列表,然后是每个产品的详细 View 。

我让应用程序按照我需要的方式顺畅地运行,但当我试图提升用户体验时,我开始遇到麻烦。为 iPad 添加 UISplitViewController 让我头疼不已并浪费了下午的时间。

目前,我收到关于委托(delegate)相关代码的语义问题报告。一个在 DetailViewController.h 中,另一个在 ListViewController.m 中。

我会在发布之前总结一下我对这段代码的意图,但由于我缺乏经验,我可能会遗漏一些细微之处:

AppDelegate 分配 UITableViewController(ListViewController 类)和 UIViewController(DetailViewController 类),然后检查 iPad。如果是 iPad,它会使用两个 View 的数组创建一个 UISplitViewController。否则它加载 ListViewController 作为主视图。

在我尝试在两个 View 之间创建委托(delegate)关系之前,应用程序构建成功,但 iPad UISplitViewController 仅加载了一个空的详细 View 。iphone 加载 ListViewController,然后选择一行显示一个空的详细 View (DetailViewController)。当您返回到 TableView 并选择相同或另一个表格单元格时,正确的信息将加载到 DetailView 中。这让我相信 TableView 的初始实例没有正确传递选择,但返回它(重新分配它?)会解决问题。我希望委托(delegate)设置能解决这个问题。由于我无法使该部分正常工作,因此我无法检验该理论。我只是想我会提到它。

关于 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

最佳答案

我认为你遇到了一个问题,编译器因为有几个而感到困惑

#import "DetailViewController.h"

如果您从 ListViewController.h 中删除此导入并保留

@class DetailViewController;

那么我认为这将解决您的编译器问题。

您可能需要添加 < UISplitViewControllerDelegate >不过,到你的其他几个类(class)。看起来您正在将他们设置为 Split View的委托(delegate),但没有采用协议(protocol)。

关于ios - UISplitView,在设置委托(delegate)时接收语义问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13423348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com