2

我密切关注这个中心页面http://klanguedoc.hubpages.com/hub/iOS-How-To-Display-Detail-Data-from-UITableView-in-Detail-View-Controller并且几乎能够复制它完美,除了最后的组件。prepareForSegue 方法。

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([[segue identifier] isEqualToString:@"showDetail"]) {
        klViewController *detailViewController = [segue destinationViewController];

        detailViewController.treeData = [self.ds objectAtIndex:[self.tableView indexPathForSelectedRow].row];

    }
}

我试图仔细查看并尝试在网上找到其他prepareForSegue方法的示例,但它们都指向同一个方向,这意味着方法和代码是正确的!

xcode 发出的错误是 ARC Issue - No visible @interface for 'NSString' 声明选择器 'isEqualToString:'

谁能指出我正确的方向?

PS 如果我错了,请纠正我,但是这个 prepareForSegue 方法是否应该在 tableview 类中?如果不是,请告诉我,因为我是 xcode 的新手,而且教程有点模糊,因此我担心我会错过解释代码应该去的地方。

提前谢谢了 :)

编辑:

样本数据.h

#import <Foundation/Foundation.h>

@interface SampleData : NSObject

@property (nonatomic,strong) NSString * treeName;
@property (nonatomic,strong) NSString * treeDescription;

@end

样本数据.m

#import "SampleData.h"

@implementation SampleData
@synthesize treeName, treeDescription;


@end

样本数据DOA.h

#import <Foundation/Foundation.h>
#import "SampleData.h"
@interface SampleDataDAO : NSObject

@property (nonatomic,strong) NSMutableArray *someDataArray;

-(NSMutableArray *)PopulateDataSource;

@end

样本数据DOA.m

#import "SampleDataDAO.h"

@implementation SampleDataDAO

@synthesize someDataArray;



-(NSMutableArray *)PopulateDataSource
{
    someDataArray= [[NSMutableArray alloc]initWithCapacity:3];
    SampleData * mydata= [[SampleData alloc]init];

    mydata.treeName = @"Oak";
    mydata.treeDescription = @"an oak is a tree or shurb bla bla bla";
    [someDataArray addObject:mydata];
    mydata=nil;

    mydata=[[SampleData alloc]init];
    mydata.treeName=@"Douglas Fir";
    mydata.treeDescription=@"Douglas Fir are medium-size to extremely large evergreen trees";
    [someDataArray addObject:mydata];
    mydata=nil;

    mydata=[[SampleData alloc]init];
    mydata.treeName=@"Sugar Maple";
    mydata.treeDescription=@"Sugar Maple is a species of Maple. Sugar Maple is best known for it's sweet leaves as it contain natural sugar and is extremely healthy and beneficial to our health";
    [someDataArray addObject:mydata];
    mydata=nil;



    return someDataArray;
}


@end

表视图控制器.h

#import <UIKit/UIKit.h>
#import "SampleDataDAO.h"
#import "SampleData.h"
#import "ViewController.h"

@interface TableViewController : UITableViewController

@property(nonatomic,strong) SampleDataDAO* daoDS;
@property (nonatomic,strong) NSMutableArray *ds;


@end

TableViewController.m(prepareForSegue 方法出错)

#import "TableViewController.h"

@interface TableViewController ()

@end

@implementation TableViewController
@synthesize daoDS,ds;

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if( [[segue identifier] isEqualtoString:@"showDetail"]){        
        ViewController * detailViewController = [segue destinationViewController];

        detailViewController.treeData=[self.ds objectAtIndex:[self.tableView indexPathForSelectedRow].row];
    }
}


- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    daoDS = [[SampleDataDAO alloc]init];
    self.ds = daoDS.PopulateDataSource;

    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return 3;
}   

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"treeCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    SampleData * sample = [self.ds objectAtIndex:indexPath.row];
    cell.textLabel.text = sample.treeName;
    NSLog(@"Cell Value %d %@", indexPath.row, cell.textLabel.text);




    return cell;
}





/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

#pragma mark - Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    /*
     <#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil];
     // ...
     // Pass the selected object to the new view controller.
     [self.navigationController pushViewController:detailViewController animated:YES];
     */
}

@end

视图控制器.h

#import <UIKit/UIKit.h>
#import "SampleData.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextView *treeInfo;
@property (nonatomic, strong) SampleData * treeData;

@end

视图控制器.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize treeData, treeInfo;

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}   

@end

PS - 代码几乎与网站中的 1 相似,主要区别在于我删除了代码的图片部分。

编辑 - 'NSString' 的错误日志文件没有可见的@interface 声明选择器'isEqualtoString:' if( [[segue identifier] isEqualtoString:@"showDetail"]){

错误下划线位于“[[” 希望这是对你们找出真正错误的有用提示._。

4

2 回答 2

1

因此,当您处理 iOS 时,您处于 MVC 架构中,这意味着您拥有控制情节提要的 ViewController,因此您的 prepareForSegue 方法应该位于与正在转换的视图对应的 ViewController 类中。

也许在右侧的身份检查器中查看您的故事板。确保“自定义类”是您正在使用的视图控制器,其中包含 prepareForSegue 方法

没有看到更多代码,很难说,但这个链接似乎有你需要的东西:“'BlahDataController'没有可见的@interface声明选择器'aMethod:'”

如果此链接不起作用,请尝试再次查看给定教程的每个步骤。你可能会发现一些东西。

干杯

于 2012-05-31T06:46:44.647 回答
1

一方面,改变:

if ([[segue identifier] isEqualToString:@"showDetail"]) {

if ([segue.identifier isEqualToString:@"showDetail"]) {

klViewController *detailViewController = [segue destinationViewController];

klViewController *detailViewController = segue.destinationViewController;

此外,我倾向于将 segue 连接到 ViewController 或 TableViewController 本身,而不是连接到 tableview 单元格。然后在 cellForRowAtIndexPath 中,我会关闭 segue 并像这样传递 IndexPath:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self performSegueWithIdentifier:@"showDetail" sender:indexPath];
}

然后使用您的 prepareForSegue 方法,我会这样做:

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    if ([segue.identifier isEqualToString:@"showDetail"]) {
        // Get the indexPath
        NSIndexPath *indexPath = (NSIndexPath *)sender;

        klViewController *detailViewController = segue.destinationViewController;
        detailViewController.treeData = [self.ds objectAtIndex:indexPath.row;
    }
}
于 2012-05-31T12:35:10.670 回答