iphone - 难住了,从类里面检索一个字符串

标签 iphone ios

我有一个自定义表格单元格类。

文本输入.h

@interface TextInput : UITableViewCell <UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate> {
    UILabel *cellLabel;
    UITextField *textFieldBox;
    NSString *imageFile;  
}

@property (nonatomic, retain) UITextField *textFieldBox;
@property (nonatomic, retain) UILabel *cellLabel;
@property (nonatomic, retain) NSString *imageFile;

它有一个相机按钮,并将图像保存到本地文档文件夹。我将文件名保存到 imageFile,它是用唯一的名称生成的。

文本输入.m

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    //retrieve image
    UIImage *image = [info objectForKey: @"UIImagePickerControllerOriginalImage"];

    imageFile = [self findUniqueSavePath];

    [self saveImage:image:imageFile];
    //dismiss the camera 
     CoreTableAppDelegate *mainDelegate = (CoreTableAppDelegate *) [[UIApplication sharedApplication] delegate];
    [mainDelegate.rootViewController dismissModalViewControllerAnimated: YES];
}

- (NSString *)findUniqueSavePath {
    NSString *path;
    CFUUIDRef uuid = CFUUIDCreate(NULL);
    NSString *uuidString = [(NSString *)CFUUIDCreateString(NULL, uuid) autorelease];
    CFRelease(uuid);
    path = [uuidString stringByAppendingPathExtension: @"png"];
    return path;
}

一切正常,除非我想从父 UItableView 类中检索文件名。

编辑坦克描述.m

- (void)getCell
{

    TextInput *cell1 = (TextInput *)[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]];    
    NSLog(@"save imageFile:%@", cell1.imageFile); <***** crashes on this line

}

它在带有 EXC_BAD_ACCESS 的 NSLog 语句上崩溃。就像 imageFile 不是 NNString 一样。如果我设置 imageFile = @"SOme random string",则没有错误。 我很困惑。

感谢您的见解。

最佳答案

imageFile = [self findUniqueSavePath];

您没有保留imageFile ivar。当你去读取字符串时,它是一个悬空指针,你会得到 EXC_BAD_ACCESS。

相反,请使用属性访问器,它将为您处理释放和保留:

self.imageFile = [self findUniqueSavePath];

或者你可以直接设置ivar,在这种情况下你需要自己进行内存管理:

[imageFile release];
imageFile = [[self findUniqueSavePath] retain];

关于iphone - 难住了,从类里面检索一个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8468629/

相关文章:

ios - 创建 View Controller

iphone - UIWebView 加载 URL 带参数

ios - 如何在 IOS 中将回调方法作为参数传递

ios - React Native 开发人员菜单未加载

ios - 通过 swift 集成新的 facebook SDK

ios - 核心数据 : Data does not store to database (swift)

iphone - 格式化 NSDate 后,新日期比实际日期早一天

ios - 较大设备中的自动布局问题

ios - 在ios中注销时删除cookie

iphone - 在 UIWebView 中呈现 html 时,iOS 应用程序变得无响应