NSDictionary转NSArray,无法正常显示?
我有一个签名页,用户可以在其中签名。 然后需要将其保存到 NSDictionary,但我想将键列表调用为 TableView 中每行或单元格的文本。 所以:
“viewImage = 保存为对象到键:随机数”
这部分有点简单,困难的部分是当我在 TableView 的另一个页面上调用它时。 它退出应用程序并出现错误“SIGABRT”。现在我的所有代表都已到位并正在工作……我相信。 现在这里是一些示例代码:
FirstPage.m
UIImagePNGRepresentation(viewImage);
NSMutableArray *innerArray = [[NSMutableArray array]init];
[innerArray addObject:viewImage];
[SignatureSave setObject:innerArray forKey:@"5599"];
足够简单,但不会给我错误。
SecondPage.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstPage *appShare = (FirstPage *)[[UIApplication sharedApplication] delegate];
NSArray *dataDuplicate = [[NSArray alloc]init ];
dataDuplicate = [appShare.SignatureSave allKeysForObject:@"innerArray"];
static NSString *CellIdentifier = @"Cell";
NSLog(@"%@",dataDuplicate);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero]autorelease];
}
if (dataDuplicate != nil) {
cell.textLabel.text = [dataDuplicate objectAtIndex:indexPath.row];
}
else
{
UIAlertView *CellAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error Loading content, Try Again Later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[CellAlert show];
[CellAlert release];
}
return cell;
}
@end
现在,我如何将 viewImage
保存到 NSDictionary,以便能够在 SecondPage
上调用它并在 TableVIew 中显示对象的名称?
I have a signature page, were the user can sign their name.
It needs to then be saved to NSDictionary, but i want to call a List of the keys to be text in a TableView for each row or cell.
so:
"viewImage = saved as object to key:Random Number"
That parts somewhat easy, the hard part is when i call it on the other Page to the TableView.
It Exits the App with Error"SIGABRT". Now all my Delegates are in place and working...i believe.
now heres some example code:
FirstPage.m
UIImagePNGRepresentation(viewImage);
NSMutableArray *innerArray = [[NSMutableArray array]init];
[innerArray addObject:viewImage];
[SignatureSave setObject:innerArray forKey:@"5599"];
simple Enough, but doesnt give me an error.
SecondPage.m
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
FirstPage *appShare = (FirstPage *)[[UIApplication sharedApplication] delegate];
NSArray *dataDuplicate = [[NSArray alloc]init ];
dataDuplicate = [appShare.SignatureSave allKeysForObject:@"innerArray"];
static NSString *CellIdentifier = @"Cell";
NSLog(@"%@",dataDuplicate);
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero]autorelease];
}
if (dataDuplicate != nil) {
cell.textLabel.text = [dataDuplicate objectAtIndex:indexPath.row];
}
else
{
UIAlertView *CellAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Error Loading content, Try Again Later." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[CellAlert show];
[CellAlert release];
}
return cell;
}
@end
Now, How do i get the viewImage
to save to the NSDictionary, to be able to call it on the SecondPage
and display the name of the objects in the TableVIew?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我真的不明白你到底有什么问题。
首先,你的字典是一个保留对象吗?
现在,如果你通过在前面写
self.
来访问该对象,它将调用retain,你的对象将保持活动状态。否则它将在方法结束时自动释放。这将解决您的字典在创建表视图时可能不存在/不可用的问题,并且您不必使用单例。您想使用此代码访问什么?
使用
[[UIApplication sharedApplication] delegate]
您可以获得应用程序委托(显然)。这些是MyAppNameAppDelegate
文件,但您将其视为FirstPage
类。只需使用
NSLog()
来检查您是否获得了正确的类,即您期望的类。这里有潜在的泄漏,您分配了初始化但从未释放它:
此外,您可以简化它(将自动释放):
这里还有另一个问题。
为什么您将对象的所有键称为
@"innerArray"
?您没有这样的对象,而且在更多情况下它是错误的。
innerArray
是您之前在FirstPage.m
中命名的数组,但这只是为了让您作为开发人员更好地记住该变量。编译后它无论如何都会有一个神秘的名字。如果您愿意,您可以访问您的密钥@"5599"
,但我认为您不想要这个。在您的情况下,您想要访问字典的所有键,因此只需调用:现在您将拥有一个包含字典所有键的数组,您可以像使用
objectAtIndex:
一样访问它们。告诉我这是否能解决您的问题,或者告诉我我是如何误解您的问题的。
I really don't understand whats your problem exactly.
first of all, is your dictionary a retained object?
now if you access the object by writing
self.
in front it will call the retain and your object will stay alive. Otherwise it would be autoreleased at the end of the method. This will fix the problem that your dictionary is maybe not present/available at table view creation and you don't have to use a singleton.what are you trying to access with this code?
with
[[UIApplication sharedApplication] delegate]
you get your application delegate (obviously). These are theMyAppNameAppDelegate
files but you treat it as aFirstPage
class.Just
NSLog()
to check you get the right class, the one you expect.here you have a potential leak, you alloc-init but never release it:
furthermore you can simplify it (will be autoreleased):
and here you have another issue.
Why do you call all keys for the object
@"innerArray"
?you don't have such an object and it's in many more cases wrong.
innerArray
was your previously named array inFirstPage.m
but it is only for you as a developer to remember the variable better. After compilation it will have a cryptic name anyway. You could access your key@"5599"
if you like but I don't think you want this. In your case you want to access all keys of the dictionary so simply call:now you will have an array with all keys of your dictionary and you can access them like you do with
objectAtIndex:
.Tell me if this solves your problems or tell me how I misunderstood your question.
我发现这个问题的答案实际上很简单,
我最终选择了单例方法而不是全局变量方法。
现在,单例方法看起来很可怕,但它很简单,请参阅此处。
我注意到单例方法与全局方法的主要区别是,
全局方法需要大量的转换和重新转换。
尽管单例方法正在处理多个页面或类上的单个对象。
现在我希望这也能更好地帮助将来的人们!
I found the answer to this to be quit simple actually,
I ended up going with the Singleton Method instead of the Global Variable Method.
Now the Singleton Method looks terrifying but its quit simple, See here.
The main difference i noticed from the singleton method to the global method is,
Global method takes a lot of converting and re-converting.
Though the Singleton Method is working with a single object over many pages or classes.
Now i hope this will better assist people in the future also!