我想做一些类似“添加到收藏夹”的技术。在我的应用中有一个tabbar
。在第一个选项卡上有一个带按钮的表单。我想要做的是当你按下按钮时,它会在数组中保存导航栏的名称并将数组保存在plist
中。当你打开第二个标签时,你会看到一个包含tableview
的视图,我希望在一个单元格中看到navigationBar
的名称(所以数组从{{1}获取信息并将其放在plist
中。所以这是我的代码:
tableView
Xcode没有显示任何错误,但是当我打开它时我的// Info.h Its a class with button
#import <UIKit/UIKit.h>
@class Favorites;
@interface Info : UIViewController
{
NSMutableArray *favGifts;
NSString *justAstring;
}
@property (nonatomic, retain) NSMutableArray *favGifts;
@property (nonatomic, retain) NSString *justAstring;
- (IBAction)save:(id)sender;
-(NSString *)pathOfFile;
@end
//Info.m
#import "Info.h"
@implementation Info
@synthesize ADFbutton;
@synthesize justAstring;
@synthesize favGifts;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (NSString *)pathOfFile {
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentFolder = [path objectAtIndex:0];
return [documentFolder stringByAppendingFormat:@"myFile.plist"];
}
- (IBAction)save:(id)sende{
NSString *m = self.navigationItem.title;
favGifts = [[NSMutableArray alloc]init];
[favGifts addObject:m];
[favGifts writeToFile:[self pathOfFile] atomically:YES];
justAstring = self.pathOfFile;
NSLog(@"%@", justAstring);
}
//And this is class with the tableview inside:
#import <UIKit/UIKit.h>
@class Info;
@interface Favorites : UIViewController
<UITableViewDataSource>{
UITableView *myTableView;
NSString *name;
Info *info;
}
@property (retain, nonatomic) IBOutlet UITableView *myTableView;
@property (nonatomic,retain) NSString *name;
@property (nonatomic, retain) Info *info;
@end
// .m
#import "Favorites.h"
#import "Info.h"
NSMutableArray *array;
@implementation NewFavGifts
@synthesize myTableView;
@synthesize giftInfo;
@synthesize name;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void) viewWillAppear:(BOOL)animated{
NSString *filePath = info.justAstring;
if ([[NSFileManager defaultManager]fileExistsAtPath:@"myFile.plist"]) {
array = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
}
[myTableView reloadData];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[self setMyTableView:nil];
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (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 [array count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell...
NSString *cellValue = [array objectAtIndex:indexPath.row];
cell.textLabel.text = cellValue;
[cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 75;
}
@end
为空。
我连四天都找不到我的错误或任何解决方案。
答案 0 :(得分:1)
你必须逐步麻烦拍摄
通过添加NSLog来检查数组中是否有任何东西,如下所示,
如果您在控制台中获得“Array Count 0”,则表示没有任何内容写入plist文件
或者如果您根本没有在控制台中获取NSLog文本,则表示不存在plist文件
- (void) viewWillAppear:(BOOL)animated{
NSString *filePath = info.justAstring;
if ([[NSFileManager defaultManager]fileExistsAtPath:@"myFile.plist"]) {
array = [[NSMutableArray alloc]initWithContentsOfFile:filePath];
NSLog(@"Array Count %d", [array count)); //add this line and check whether you get any thing in the array
}
[myTableView reloadData];
}
答案 1 :(得分:1)
我有几件事要改变:
在接口定义中,添加UITableviewDelegate
@interface Favorites:UIViewController&lt; UITableViewDataSource,UITableViewDelegate&gt; {
将您的NSArray *array
放入界面,添加属性nonatomic,retain
用self.array = [NSArray arrayWithContentsOfFile:...]
填充数组。它不需要是NSMutableArray,而是使用self.array
来正确保留它。