我有以下问题。我创建了一个功能齐全的浏览器,但是当我使用“添加书签功能”或“显示书签功能”时,我总是得到一个Thread1:signal SIGABRT错误。我从8个小时开始尝试了一切,但我找不到解决办法。有人可以看看我的代码给我一个线索如何解决这个问题?我非常沮丧,希望有人可以提供帮助,因为我真的不知道该怎么做。我认为它与“forKey”标签有关,但我不确定。这是我的代码:
AppDelegate.h:
#import <UIKit/UIKit.h>
@class ViewController;
@interface AppDelegate : NSObject <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m:
#import "AppDelegate.h"
#import "ViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSMutableDictionary* dictionary = [NSMutableDictionary dictionary];
[dictionary setObject:[NSMutableArray array] forKey:@"medis"];
[[NSUserDefaults standardUserDefaults] registerDefaults:dictionary];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
}
- (void)applicationWillEnterForeground:(UIApplication *)application {
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
@end
ViewController.h:
#import <UIKit/UIKit.h>
#import "FavoritenController.h"
@interface ViewController : UIViewController <UITextFieldDelegate, UIWebViewDelegate, UIActionSheetDelegate>
{
UIWebView *Webseite;
UIBarButtonItem *backButton;
UIBarButtonItem *forwardButton;
UITextField *textField;
UIBarButtonItem *refreshButton;
NSString* urlBeforeEditing;
FavoritenController* favoritenController;
}
@property (nonatomic, retain) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *onlinetauscher;
@property (nonatomic, retain) IBOutlet UIWebView *Webseite;
@property ( nonatomic, retain) IBOutlet UIBarButtonItem *refreshButton;
@property ( nonatomic, retain) IBOutlet UIBarButtonItem *backButton;
@property (nonatomic, retain) IBOutlet UIBarButtonItem *forwardButton;
- (IBAction)onlinetauscher:(UIBarButtonItem *)sender;
- (IBAction)pressRefresh:(UIBarButtonItem *)sender;
- (IBAction)zeigeMedikamente:(id)sender;
- (IBAction)hinzufuegen:(id)sender;
@end
ViewController.m:
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize Webseite;
@synthesize refreshButton;
@synthesize textField;
@synthesize backButton;
@synthesize forwardButton;
- (void)loadString:(NSString*)urlString{
NSRange range = [urlString rangeOfString:@"."];
NSURLRequest* request;
if (range.location != NSNotFound) {
if ([urlString hasPrefix:@"http://"] == YES || [urlString hasPrefix:@"https://"] == YES) {
request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
} else {
NSString* string = [NSString stringWithFormat:@"https://%@", urlString];
request = [NSURLRequest requestWithURL:[NSURL URLWithString:string]];
}
} else {
NSString* tempString = [urlString stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
NSString* string = [NSString stringWithFormat:@"https://www.google.de/search?q=%@", tempString];
request = [NSURLRequest requestWithURL:[NSURL URLWithString:string]];
}
[Webseite loadRequest:request];
}
-(void)loadBookmark:(NSString*)urlString {
[self loadString:urlString];
self.textField.text = urlString;
}
-(void)cancel {
[textField resignFirstResponder];
}
#pragma mark -
#pragma mark Delegates
-(BOOL)textFieldShouldReturn:(UITextField *)atextField { //?
[self loadString:textField.text];
[textField resignFirstResponder];
return YES;
}
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)
navigationType{
textField.text = [request.URL absoluteString];
[textField resignFirstResponder];
return YES;
}
-(void) webViewDidStartLoad:(UIWebView *)awebView{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void) webViewDidFinishLoad:(UIWebView *)awebView{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
if ([awebView canGoBack] == YES){
[backButton setEnabled:YES];
}else{
[backButton setEnabled:NO];
}
if([awebView canGoForward] == YES){
[forwardButton setEnabled:YES];
}else{
[forwardButton setEnabled:NO];
}
}
#pragma mark - View lifecycle
- (void)viewDidLoad {
[super viewDidLoad];
forKeyPath:@"contentOffset" options:0 context:NULL]; //erzeugt sigabrt
favoritenController = [[FavoritenController alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"];
NSURL *scriptUrl = [NSURL URLWithString:@"thewebsite"];
NSData *urlData = [NSData dataWithContentsOfURL:scriptUrl];
if (urlData) {
NSLog(@"Device is connected to the internet");
NSURL *myURL = [NSURL URLWithString:@"thewebsite"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[Webseite loadRequest:myRequest];
}
else {
NSLog(@"Device is not connected to the internet");
[urlData writeToFile:filePath atomically:YES];
[Webseite loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
}
}
- (IBAction)onlinetauscher:(UIBarButtonItem *)sender {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:@"%@/%@", [paths objectAtIndex:0],@"index.html"];
NSURL *scriptUrl = [NSURL URLWithString:@"thewebsite"];
NSData *urlData = [NSData dataWithContentsOfURL:scriptUrl];
if (urlData) {
NSLog(@"Device is connected to the internet");
NSURL *myURL = [NSURL URLWithString:@"thewebsite"];
NSURLRequest *myRequest = [NSURLRequest requestWithURL:myURL];
[Webseite loadRequest:myRequest];
}
else {
NSLog(@"Device is not connected to the internet");
[urlData writeToFile:filePath atomically:YES];
[Webseite loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
}
}
- (void)viewDidUnload
{
[self setWebseite:nil];
[self setTextField:nil];
[self setRefreshButton:nil];
[self setBackButton:nil];
[self setForwardButton:nil];
[super viewDidUnload];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (IBAction)pressRefresh:(UIBarButtonItem *)sender {
[self loadString:textField.text];
}
- (IBAction)zeigeMedikamente:(UIBarButtonItem *)sender {
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:favoritenController];
favoritenController.delegatee = self;
[self presentModalViewController:navController animated:YES];
[favoritenController newMethod];
}
- (IBAction)hinzufuegen:(UIBarButtonItem *)sender {
NSString* title = [Webseite stringByEvaluatingJavaScriptFromString:@"document.title"];
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:title, @"title", textField.text, @"url", nil];
[favoritenController.medis addObject:dict];
[favoritenController.tableView reloadData];
UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"Bookmark added" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alertView show];
[[NSUserDefaults standardUserDefaults] setObject:favoritenController.medis forKey:@"medis"];
}
@end
FavoritenController.h:
#import <UIKit/UIKit.h>
@interface FavoritenController : UITableViewController {
NSMutableArray* medis;
__unsafe_unretained NSObject* delegatee;
}
@property (nonatomic, retain) NSMutableArray* medis;
@property (nonatomic, assign) NSObject* delegatee;
-(void) newMethod;
@end
FavoritenController.m:
#import "FavoritenController.h"
@interface FavoritenController ()
@end
@implementation FavoritenController
@synthesize medis, delegatee;
-(id)init
{
self = [super init];
if(self){
if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"medis"] count] != 0) {
self.medis = [[NSUserDefaults standardUserDefaults] objectForKey:@"medis"];
} else {
self.medis = [[NSMutableArray alloc] init];
}
}
return self;
}
- (void)dealloc
{
self.medis = NULL;
}
- (void) cancel {
[self dismissModalViewControllerAnimated:YES];
}
#pragma mark - View lifecycle
-(BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return YES;
}
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
NSDictionary* dict = [medis objectAtIndex:sourceIndexPath.row];
[medis removeObjectAtIndex:sourceIndexPath.row];
[medis insertObject:dict atIndex:destinationIndexPath.row];
[[NSUserDefaults standardUserDefaults] setObject:self.medis forKey:@"medis"];
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
if (editingStyle == UITableViewCellEditingStyleDelete) {
[medis removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
[[NSUserDefaults standardUserDefaults] setObject:self.medis forKey:@"medis"]; }
}
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
return UITableViewCellEditingStyleDelete;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"Meine Medikamente";
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action: @selector(cancel)]];
medis = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"medis"] mutableCopy];
[self.tableView reloadData];
[self.navigationItem setRightBarButtonItem:self.editButtonItem];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [medis count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [[medis objectAtIndex:indexPath.row ] objectForKey:@"title"];
cell.detailTextLabel.text = [[medis objectAtIndex:indexPath.row] objectForKey:@"url"];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString* url = [[medis objectAtIndex:indexPath.row] objectForKey:@"url"];
#pragma GCC diagnostic ignored "-Wundeclared-selector"
if (self.delegatee && [self.delegatee respondsToSelector:@selector(loadBookmark:)]) {
[self.delegatee performSelector:@selector(loadBookmark:) withObject:url];
}
[self cancel];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void) newMethod{
self.navigationItem.title = @"My Bookmarks";
[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action: @selector(cancel)]];
medis = [[[NSUserDefaults standardUserDefaults] arrayForKey:@"medis"] mutableCopy];
[self.tableView reloadData];
}
@end
调试控制台:
* thread #1: tid = 0x2e6a, 0x0000000104ccc002 libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
frame #0: 0x0000000104ccc002 libsystem_kernel.dylib`__pthread_kill + 10
frame #1: 0x0000000104c925c5 libsystem_pthread.dylib`pthread_kill + 90
frame #2: 0x0000000104a32cec libsystem_c.dylib`abort + 129
frame #3: 0x0000000104831051 libc++abi.dylib`abort_message + 257
frame #4: 0x0000000104856ac9 libc++abi.dylib`default_terminate_handler() + 267
frame #5: 0x0000000101cca046 libobjc.A.dylib`_objc_terminate() + 103
frame #6: 0x000000010485426e libc++abi.dylib`std::__terminate(void (*)()) + 8
frame #7: 0x0000000104853ef9 libc++abi.dylib`__cxa_rethrow + 99
frame #8: 0x0000000101cc9f5e libobjc.A.dylib`objc_exception_rethrow + 40
frame #9: 0x00000001021718e4 CoreFoundation`CFRunLoopRunSpecific + 676
frame #10: 0x0000000105a0dad2 GraphicsServices`GSEventRunModal + 161
frame #11: 0x00000001025fd610 UIKit`UIApplicationMain + 171
* frame #12: 0x00000001017c5ddf Websitemobil`main(argc=1, argv=0x00007fff5e43c640) + 111 at main.m:14
frame #13: 0x000000010498c92d libdyld.dylib`start + 1