Iphone iOS将数据从tableview传递到新视图

Iphone iOS将数据从tableview传递到新视图,iphone,ios,xcode,Iphone,Ios,Xcode,我密切关注这个页面,除了最后一个组件外,我几乎可以完美地复制它。制备法 -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([[segue identifier] isEqualToString:@"showDetail"]) { klViewController *detailViewController = [segue destinationViewController];

我密切关注这个页面,除了最后一个组件外,我几乎可以完美地复制它。制备法

-(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问题-“NSString”没有可见的@interface声明选择器“isEqualToString:”

谁能给我指一下正确的方向吗

如果我错了,请纠正我,但是这个prepareforsgue方法应该在tableview类中吗?如果不是,请让我知道,因为我是xcode新手,而且教程有点模糊,因此我恐怕无法解释代码应该放在哪里

非常感谢:)

编辑:

SampleData.h

#import <Foundation/Foundation.h>

@interface SampleData : NSObject

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

@end
SampleDataDOA.h

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

@property (nonatomic,strong) NSMutableArray *someDataArray;

-(NSMutableArray *)PopulateDataSource;

@end
TableViewController.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
#import <UIKit/UIKit.h>
#import "SampleData.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UITextView *treeInfo;
@property (nonatomic, strong) SampleData * treeData;

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

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


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

因此,当你们处理iOS时,你们处于MVC体系结构中,这意味着你们拥有控制故事板的ViewController,所以你们的PrepareForsgue方法应该在与正在转换的视图相对应的ViewController类中

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

如果看不到更多的代码,很难说,但是这个链接似乎有你需要的:

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

为一件事干杯,改变:

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

另外,我倾向于将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;
    }
}

我已经编辑并添加了代码。我花了几天的时间仔细阅读了教程的步骤,甚至两次重做了整个教程,但最终还是遇到了同样的问题。好的,谢谢!我明天早上到达工作站时第一件事就是尝试。如果我更改为detailViewController.treedata=[self.ds objectAtIndex:(nsindepath*)sender];我会得到一个警告:整数转换指针不兼容,将“nsindepath”发送到类型为“NSUInteger”(也称为“unsigned int”)的参数。红色错误仍然存在。OP没有说明,但可以安全地假设它代表“数据源”是NSArray或NSMutableArray。这篇文章可能会有帮助:
#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
if ([[segue identifier] isEqualToString:@"showDetail"]) {
if ([segue.identifier isEqualToString:@"showDetail"]) {
klViewController *detailViewController = [segue destinationViewController];
klViewController *detailViewController = segue.destinationViewController;
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self performSegueWithIdentifier:@"showDetail" sender:indexPath];
}
-(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;
    }
}