Я пытался добавить в приложение простую закладку для своей диссертации в универе. Я новичок в разработке для iPhone, поэтому на самом деле я не понимаю, что происходит.
То, что я пытаюсь сделать, это добавить объект (строку) в массив, но то, что я сделал, вообще не работает.
Я установил свой массив в AppDelegate, и он заполняет табличное представление в файле BookmarksViewController.m.
Мое приложение состоит из панели вкладок и панели навигации, но я выделил все это здесь, чтобы его было легче читать.
Вот код:
НАСТРОЙКА Массива
DissertationAppDelegate.h
#import <UIKit/UIKit.h>
@class DissertationViewController;
@interface DissertationAppDelegate : NSObject <UIApplicationDelegate> {
NSMutableArray *array;
UIWindow *window;
DissertationViewController *viewController;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) NSMutableArray *array;
@end
DissertationAppDelegate.m
#import "DissertationAppDelegate.h"
@implementation DissertationAppDelegate
@synthesize window;
@synthesize array;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *) launchOptions {
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [NSString stringWithFormat:@"%@/arraySaveFile", documentsDirectory];
array = [[NSMutableArray alloc] init];
[array addObject:@"One"];
[array addObject:@"Two"];
[array addObject:@"Three"];
[array writeToFile:fullFileName atomically:NO];
[self.window makeKeyAndVisible];
}
НАСЕЛЕНИЕ ВИДА НА СТОЛ С МАССОЙ
BookmarksViewController.h
@interface BookmarksViewController : UITableViewController {
NSMutableArray *bookmarksArray;
}
@property (nonatomic, retain) NSMutableArray *bookmarksArray;
BookmarksViewController.m
#import "BookmarksViewController.h"
@synthesize bookmarksArray;
- (void)viewDidLoad {
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullFileName = [NSString stringWithFormat:@"%@/arraySaveFile", documentsDirectory];
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:fullFileName];
bookmarksArray = array;
[self.tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [bookmarksArray 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] autorelease];
}
// Configure the cell...
cell.textLabel.text = [bookmarksArray objectAtIndex:indexPath.row];
return cell;
}
- (void)dealloc {
[bookmarksArray release];
[super dealloc];
}
ВЫПОЛНЕНИЕ ИБАКЦИИ ОТ ДРУГОГО КОНТРОЛЛЕРА ВИДА
Заголовочный файл
-(IBAction) pushChap01Bookmark:(id)sender;
Файл реализации
-(IBAction) pushChap01Bookmark:(id)sender{
DissertationAppDelegate *appDelegate = (DissertationAppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.array = [[NSMutableArray alloc] init];
[appDelegate.array addObject:@"THIS IS WHAT I WANT TO ADD"];
[self.navigationController pushViewController:bookmarksViewController animated:YES];
bookmarksViewController.title = @"Bookmarks";
}
Okkkkkkkkk .... Поэтому, когда вы нажимаете кнопку, я хотел добавить ее в массив в делегате приложения и поместить BookmarksViewController в стек навигации и обновить таблицу. Делает все, кроме добавления объекта в массив.
Если есть кто-нибудь, кто знает, что случилось, тогда, пожалуйста, помогите мне. Я буду очень благодарен.
Спасибо