获取成员“indexPath”的错误请求编译时不是结构或联合的东西

发布于 2024-10-16 02:34:34 字数 1548 浏览 7 评论 0原文

我有一个由表格视图示例制作的应用程序,我设法获取表格上列出的文档目录的文件,并且还获得了在详细视图上查看其内容的方法,现在当我编辑一个文件并且我想要时出现问题保存它。 当我想访问 RootViewController 上的表视图的 indexPath.row 时,出现错误:在非结构或联合中请求成员“indexPath”。这是 DetaiViewController 的标头:

#import <UIKit/UIKit.h>
#import "RootViewController.h"
@class RootViewController;

@interface DetailViewController : UIViewController 
{
    IBOutlet UITextView *labelName;
    NSString *strName;
}
@property (nonatomic, retain) NSString *strName;
@property (nonatomic, retain) UITextView *labelName;

- (IBAction)saveText:(id)sender;
- (IBAction)hideKeyb:(id)sender;
@end

这是我遇到麻烦的 IBOutlet:

- (IBAction)saveText:(id)sender 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *data = labelName.text;
    //RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    RootViewController *rootViewController = [RootViewController alloc]
    NSString *filename = [NSString stringWithFormat:@"%d.txt",rootViewController.indexPath.row+1];
    NSString *wheresave = [documentsDirectory stringByAppendingPathComponent:filename];
    NSData *aData = [data dataUsingEncoding:NSUTF8StringEncoding];
    [aData writeToFile:wheresave atomically:YES];
}

错误位于 NSString *filename = [NSString stringWithFormat:@"%d.txt",rootViewController.indexPath.row+1 ];

我想我已经完成了所有导入,但我不确定:) 提前致谢!

I have an app made from tableview example, and I managed to get the files of documents directory listed on the table, and also got the way for seeing it's content on the detailview, now the problem comes when I edited one file and I want to save it.
I get the error: request for member 'indexPath' in something not a structure or union when I want to access indexPath.row of the table view which is on RootViewController. This is the header for DetaiViewController:

#import <UIKit/UIKit.h>
#import "RootViewController.h"
@class RootViewController;

@interface DetailViewController : UIViewController 
{
    IBOutlet UITextView *labelName;
    NSString *strName;
}
@property (nonatomic, retain) NSString *strName;
@property (nonatomic, retain) UITextView *labelName;

- (IBAction)saveText:(id)sender;
- (IBAction)hideKeyb:(id)sender;
@end

And here is the IBOutlet in where I'm having trouble:

- (IBAction)saveText:(id)sender 
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *data = labelName.text;
    //RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil];
    RootViewController *rootViewController = [RootViewController alloc]
    NSString *filename = [NSString stringWithFormat:@"%d.txt",rootViewController.indexPath.row+1];
    NSString *wheresave = [documentsDirectory stringByAppendingPathComponent:filename];
    NSData *aData = [data dataUsingEncoding:NSUTF8StringEncoding];
    [aData writeToFile:wheresave atomically:YES];
}

The error is at NSString *filename = [NSString stringWithFormat:@"%d.txt",rootViewController.indexPath.row+1];

I think I have all the imports ok, but i'm not sure :) Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2024-10-23 02:34:34

我不知道为什么你要分配 RootViewController 因为它应该已经分配给你所说的。

如果你想访问 RootViewController 中的一行(假设这是选定的一行,如果你愿意的话,你可以检索任何特定的一行)(假设它是从 UITableViewController 扩展的),它应该是这样的:

NSIndexPath *indexPath =
[rootViewController.tableView
[indexPathForSelectedRow];

NSString
*文件名 = [NSString stringWithFormat:@"%d.txt",indexPath.row+1];

I'm not sure why you're allocating RootViewController since it should be already allocated for what you're saying.

If you want to access a row (let's say that's the selected one, you can retrive any specific one if you want) in the RootViewController (assuming it's extending from UITableViewController) it should be something like this:

NSIndexPath *indexPath =
[rootViewController.tableView
indexPathForSelectedRow];

NSString
*filename = [NSString stringWithFormat:@"%d.txt",indexPath.row+1];

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文