我有一个带有按钮的主视图,当按下该按钮时,它将 Web 视图(如书签)中的 url 添加到表视图中。唯一的问题是它没有加载。我已经在这几个小时了,但无法弄清楚。我想我在表格视图中加载数据错误但不确定。
这是 MainViewController.m
- (IBAction)bookmark:(id)sender {
UIActionSheet *actionSheet =
[[UIActionSheet alloc] initWithTitle:[[[[self webView] request] URL]
absoluteString] delegate:nil cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil otherButtonTitles:@"Add", nil];
[actionSheet showInView:self.view];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
NSMutableArray *bookmarks = [[[NSUserDefaults standardUserDefaults]
arrayForKey:@"Bookmarks"] mutableCopy];
if (!bookmarks) {
bookmarks = [[NSMutableArray alloc] init];
}
[bookmarks addObject:[[[[self webView]request] URL] absoluteString]];
[[NSUserDefaults standardUserDefaults] setObject:bookmarks
forKey:@"Bookmarks"];
}
}
这是 BookmarksViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@", bookmarks);
bookmarks = [[[NSUserDefaults standardUserDefaults]
arrayForKey:@"Bookmarks"] mutableCopy];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
[[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:@"Bookmarks"];
cell.textLabel.text = [self.bookmarks objectAtIndex:indexPath.row];
return cell;
}