2012-04-12 20 views
1

申し訳ありません、これはiPhone開発の初心者なので簡単な修正です。私の代理人でiPhone開発のEXC_BAD_ACCESS

、作成したプロファイルのボタンを押した後、作成したプロファイルビューがプッシュされる:

-(void) createProfile_clicked:(id)sender 
{ 


    AddNewProfile *create = [[AddNewProfile alloc] init]; 


    [self.window addSubview:create.view]; 

    [self invisibleCreateProfileBar]; 


    AddNewProfile *controller = [[AddNewProfile alloc] initWithNibName:@"AddNewProfile" bundle:[NSBundle mainBundle]]; 
    [self.navigationController pushViewController:controller animated:YES ]; 

    currentController=controller; 
} 

を次にAddNewProfile.mに:

- (IBAction)backgroundTap:(id)sender { 
if([nameField isFirstResponder]){ 
    [nameField resignFirstResponder]; 
} 

if([ageField isFirstResponder]){ 
    [ageField resignFirstResponder]; 
} 

if([doctorNameField isFirstResponder]){ 
    [doctorNameField isFirstResponder]; 
} 

if([doctorNumberField isFirstResponder]){ 
    [doctorNumberField resignFirstResponder]; 
} 
    } 

これは、すべてのEXC_BAD_ACCESSエラーにつながりますFirstResponderが私のコントロールのいずれかと一緒にいたずらされた時間。私はコントロール(テキストボックス)を選択することができますが、一度クリックするとクラッシュします。

ご協力いただければ幸いです。

フィールドを保持する必要がありますか?皆さんはもっとコードを必要としていますか?申し訳ありませんが、私はこのすべてに本当に新しいです。 :/


EDIT:

#import "AddNewProfile.h" 
#import "BIDDailyMedsAppDelegate.h" 
#import "BIDDailyMedsAppDelegate.h" 

@implementation AddNewProfile 
@synthesize nameField; 
@synthesize ageField; 
@synthesize doctorNameField; 
@synthesize doctorNumberField; 
@synthesize saveButton; 
@synthesize sexField; 

- (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)viewDidLoad 
{ 
    [super viewDidLoad]; 


    //[nameField becomeFirstResponder]; 
    // Do any additional setup after loading the view from its nib. 
} 

- (void)viewDidUnload 
{ 
    [self setNameField:nil]; 
    [self setAgeField:nil]; 
    [self setDoctorNameField:nil]; 
    [self setDoctorNumberField:nil]; 
    [self setSaveButton:nil]; 
    [self setSexField: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); 
} 

- (IBAction)pressSave:(id)sender { 

    BIDDailyMedsAppDelegate *appDelegate = 
    [[UIApplication sharedApplication] delegate]; 

    NSManagedObjectContext *context = 
    [appDelegate managedObjectContext]; 
    NSManagedObject *newContact; 
    newContact = [NSEntityDescription 
        insertNewObjectForEntityForName:@"Profiles" 
        inManagedObjectContext:context]; 
    [newContact setValue:self.nameField.text forKey:@"name"]; 
    [newContact setValue:self.ageField.text forKey:@"age"]; 
    if(self.sexField.selected==0){ 
     [newContact setValue:@"YES" forKey:@"male"]; 
    }else{ 
     [newContact setValue:@"NO" forKey:@"male"]; 
    } 
    [newContact setValue:self.doctorNameField.text forKey:@"doctorName"]; 
    [newContact setValue:self.doctorNumberField.text forKey:@"doctorPhone"]; 

    NSError *error; 
    [context save:&error]; 

    //Pop view off stack 
    // locally store the navigation controller since 
    // self.navigationController will be nil once we are popped 
    UINavigationController *navController = self.navigationController; 

    // Pop this controller and replace with another 
    [navController popToRootViewControllerAnimated:YES]; 

    //UIViewController *rootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 

    //[navController pushViewController:rootController animated:YES]; 
} 

- (IBAction)backgroundTap:(id)sender { 
    //AppDelegate = [[UIApplication sharedApplication] delegate]; 
    //[AppDelegate clearCreateProfileFields]; 

    //UITextField *currentTextField=sender; 
    //[currentTextField resignFirstResponder]; 

    [self.view endEditing: YES]; 

} 
@end 
+0

ブレークポイントを入れてお知らせくださいあなたがEXC_BAD_ACCESSを与える行を正確に示します。そうでなければ、他の人から無作為に仮定された答えが得られます。 –

+0

したがって、私は打撃されないbagroundTapを含むすべての関数の始めにブレークポイントを置きます。どのようなブレークポイントがそのファイルに入っているかわからないのですか? – gkres121

答えて

2

ALL CODE二つのファイル

appdelegate.mここ

#import "BIDDailyMedsAppDelegate.h" 
#import "RootViewController.h" 
#import "ProfileHomeViewController.h" 
#import "AddNewProfile.h" 
#import "BIDMedicationEditViewController.h" 
#import "BIDMedicationListViewController.h" 

@implementation BIDDailyMedsAppDelegate 

@synthesize window = _window; 
@synthesize navigationController; 

UIToolbar *toolbar; 

NSString *currentProfileName = @""; 

@synthesize managedObjectContext = __managedObjectContext; 
@synthesize managedObjectModel = __managedObjectModel; 
@synthesize persistentStoreCoordinator = __persistentStoreCoordinator; 

AddNewProfile *currentController; 

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 

    sleep(5); 

    UIViewController *rootController = [[RootViewController alloc] initWithNibName:@"RootViewController" bundle:nil]; 

    navigationController = [[UINavigationController alloc] initWithRootViewController:rootController]; 

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 



    [self.window addSubview:navigationController.view]; 


    //Initialize the toolbar 
    toolbar = [[UIToolbar alloc] init]; 
    toolbar.barStyle = UIBarStyleDefault; 

    //Set the toolbar to fit the width of the app. 
    [toolbar sizeToFit]; 

    //Caclulate the height of the toolbar 
    CGFloat toolbarHeight = [toolbar frame].size.height; 

    //Get the bounds of the parent view 
    CGRect rootViewBounds = self.navigationController.view.bounds; 

    //Get the height of the parent view. 
    CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds); 

    //Get the width of the parent view, 
    CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds); 

    //Create a rectangle for the toolbar 
    CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight); 

    //Reposition and resize the receiver 
    [toolbar setFrame:rectArea]; 

    //Create a button 
    UIBarButtonItem *createButton = [[UIBarButtonItem alloc] initWithTitle:@"Create Profile" style:UIBarButtonItemStyleBordered target:self action:@selector(createProfile_clicked:)]; 

    [toolbar setItems:[NSArray arrayWithObjects:createButton,nil]]; 

    //Add the toolbar as a subview to the navigation controller. [self.navigationController.view addSubview:toolbar]; 

    [self.window addSubview:toolbar]; 
    //[[self tableView] reloadData]; 
    // Override point for customization after application launch. 
    self.window.backgroundColor = [UIColor whiteColor]; 
    [self.window makeKeyAndVisible]; 
    return YES; 
} 

-(NSString*) getCurrentName{ 
    return currentProfileName; 
} 
-(void) setCurrentName:(NSString*)name{ 
    currentProfileName=name; 
} 

-(void) createProfile_clicked:(id)sender { 

    //[self.navigationController popViewControllerAnimated:YES]; 
    //[toolbar removeFromSuperview]; 
    AddNewProfile *create = [[AddNewProfile alloc] init]; 

    //[ removeFromSuperview]; 
    [self.window addSubview:create.view]; 
    //[toolbar removeFromSuperview]; 
    //[toolbar setOpaque:true]; 
    [self invisibleCreateProfileBar]; 

    //[creater pushViewController: animated:YES]; 

    AddNewProfile *controller = [[AddNewProfile alloc] initWithNibName:@"AddNewProfile" bundle:[NSBundle mainBundle]]; 
    [ self.navigationController pushViewController:controller animated:YES ]; 
    //[self.navigationController.view addSubview:controller]; 

    //[self.navigationController presentModalViewController:controller animated:YES]; 
    //currentController=controller; 
    //[currentController.nameField becomeFirstResponder]; 

} 

-(void) clearCreateProfileFields{ 

    if([currentController.nameField isFirstResponder]){ 
     [currentController.nameField resignFirstResponder]; 
    } 

    if([currentController.ageField isFirstResponder]){ 
     [currentController.ageField resignFirstResponder]; 
    } 

    if([currentController.doctorNameField isFirstResponder]){ 
     [currentController.doctorNameField isFirstResponder]; 
    } 

    if([currentController.doctorNumberField isFirstResponder]){ 
     [currentController.doctorNumberField resignFirstResponder]; 
    } 
    //[self becomeFirstResponder]; 
} 

-(void) addMedication_clicked:(id)sender{ 
    //BIDMedicationEditViewController *editMed = [[BIDMedicationEditViewController alloc] init]; 

    //[self.window addSubview:editMed.view]; 
    //[self.navigationController pushViewController:self.profileHomeController animated:YES]; 

    BIDMedicationEditViewController *controller = [[BIDMedicationEditViewController alloc] initWithNibName:@"MedicationEditView" bundle:[NSBundle mainBundle]]; 
    [self.navigationController pushViewController:controller animated:YES]; 

} 

-(void) medicationMenu_clicked:(id)sender{ 
    //BIDMedicationListViewController *viewMed = [[BIDMedicationListViewController alloc] init]; 
    //[self.window addSubview:viewMed.view]; 
    //[self.navigationController pushViewController: animated:<#(BOOL)#>]; 


    BIDMedicationListViewController *controller = [[BIDMedicationListViewController alloc] initWithNibName:@"MedicationListView" bundle:[NSBundle mainBundle]]; 
    [self.navigationController pushViewController:controller animated:YES]; 
    //[controller release]; 
} 

-(void) invisibleCreateProfileBar 
{ 
    //[toolbar removeFromSuperview]; 
    [toolbar setHidden:true]; 
} 

-(void) visibleCreateProfileBar 
{ 
    [toolbar setHidden:false]; 
} 

- (void)applicationWillResignActive:(UIApplication *)application 
{ 
    /* 
    Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 
    Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
    */ 
} 

- (void)applicationDidEnterBackground:(UIApplication *)application 
{ 
    /* 
    Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 
    */ 
} 

- (void)applicationWillEnterForeground:(UIApplication *)application 
{ 
    /* 
    Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. 
    */ 
} 

- (void)applicationDidBecomeActive:(UIApplication *)application 
{ 
    /* 
    Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 
    */ 
} 

- (void)applicationWillTerminate:(UIApplication *)application 
{ 
    // Saves changes in the application's managed object context before the application terminates. 
    [self saveContext]; 
} 

- (void)saveContext 
{ 
    NSError *error = nil; 
    NSManagedObjectContext *managedObjectContext = self.managedObjectContext; 
    if (managedObjectContext != nil) 
    { 
     if ([managedObjectContext hasChanges] && ![managedObjectContext save:&error]) 
     { 
      /* 
      Replace this implementation with code to handle the error appropriately. 

      abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 
      */ 
      NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
      abort(); 
     } 
    } 
} 

#pragma mark - Core Data stack 

/** 
Returns the managed object context for the application. 
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application. 
*/ 
- (NSManagedObjectContext *)managedObjectContext 
{ 
    if (__managedObjectContext != nil) 
    { 
     return __managedObjectContext; 
    } 

    NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator]; 
    if (coordinator != nil) 
    { 
     __managedObjectContext = [[NSManagedObjectContext alloc] init]; 
     [__managedObjectContext setPersistentStoreCoordinator:coordinator]; 
    } 
    return __managedObjectContext; 
} 

/** 
Returns the managed object model for the application. 
If the model doesn't already exist, it is created from the application's model. 
*/ 
- (NSManagedObjectModel *)managedObjectModel 
{ 
    if (__managedObjectModel != nil) 
    { 
     return __managedObjectModel; 
    } 
    NSURL *modelURL = [[NSBundle mainBundle] URLForResource:@"DailyMeds_withCoreData" withExtension:@"momd"]; 
    __managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL]; 
    return __managedObjectModel; 
} 

/** 
Returns the persistent store coordinator for the application. 
If the coordinator doesn't already exist, it is created and the application's store added to it. 
*/ 
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator 
{ 
    if (__persistentStoreCoordinator != nil) 
    { 
     return __persistentStoreCoordinator; 
    } 

    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"DailyMeds_withCoreData.sqlite"]; 

    NSError *error = nil; 
    __persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]]; 
    if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) 
    { 
     /* 
     Replace this implementation with code to handle the error appropriately. 

     abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. 

     Typical reasons for an error here include: 
     * The persistent store is not accessible; 
     * The schema for the persistent store is incompatible with current managed object model. 
     Check the error message to determine what the actual problem was. 


     If the persistent store is not accessible, there is typically something wrong with the file path. Often, a file URL is pointing into the application's resources directory instead of a writeable directory. 

     If you encounter schema incompatibility errors during development, you can reduce their frequency by: 
     * Simply deleting the existing store: 
     [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil] 

     * Performing automatic lightweight migration by passing the following dictionary as the options parameter: 
     [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]; 

     Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration Programming Guide" for details. 

     */ 
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 
     abort(); 
    }  

    return __persistentStoreCoordinator; 
} 

#pragma mark - Application's Documents directory 

/** 
Returns the URL to the application's Documents directory. 
*/ 
- (NSURL *)applicationDocumentsDirectory 
{ 
    return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; 
} 

@end 

FROM作成profile.mファイルがあります

通常は仕事が難しいを有効にせずにEXC_BAD_ACCESSが発生する場所。それをオンにしてもう一度テストしてみてください。

オンにする方法については、How can I set NSZombiesEnabled in XCode4?を参照してください。

0

この

- (IBAction)backgroundTap:(id)sender { 
      [self.view endEditing:YES]; 
     } 

てみてくださいuのためにこの作品。あなたのコードでは意味がありません

- (IBAction)backgroundTap:(id)sender { 
    [sender resignFirstResponder]; 
    } 

この

if([doctorNameField isFirstResponder]){ 
    [doctorNameField isFirstResponder]; 
} 
+0

申し訳ありませんでしたが、元々はコードがありましたが、別のものを試してみました。 – gkres121

+0

[self.view endEditing:YES]; – dayitv89

+0

yupそれもやった、あなたがはいを意味したことを理解した – gkres121

0

あなたが最初の応答者である編集フィールドを知っているが、それを辞任したくない場合は、この呼び出しを使用する必要があります

をアップルのドキュメントから
[self.view endEditing: NO]; 

このメソッドは、トイレ現在のビューではks、サブビュー階層では の場合は、最初のレスポンダのテキストフィールドになります。それが見つかると、 は、そのテキストフィールドを最初のレスポンダとして辞任するように要求します。 パラメータがYESに設定されている場合、テキストフィールドに入力することはありません。あなたはbackgroundTapではなく、メモリの未知のブロックに対して呼び出されている意味ビューコントローラへの参照を放出することができるように はあなたが提供した新しい情報に基づいて

+0

- (IBAction)backgroundTap:(id)送信者{ [self.view endEditing:YES]; }まだ同じエラー – gkres121

+0

2012-04-12 22の取得:10:24.611 DailyMeds_withCoreData [13061:FB03を] - [__ NSCFArray backgroundTap:]:認識されないセレクターインスタンス0x6ba0e80に 2012-04-12 22時10送信: 24.808 DailyMeds_withCoreData [13061:fb03]キャッチされていない例外 'NSInvalidArgumentException'のためアプリを終了します。理由: ' - [__ NSCFArray backgroundTap:]:インスタンスに送信された認識できないセレクタ0x6ba0e80' 新しい問題(SIGABRT)はありますか? – gkres121

+0

何らかの理由で、今度は再びEXC_BAD_ACCESSを取得することに戻ります。しかし、たぶんその1つの時間差が実際の問題のいくつかの洞察を与えることができます – gkres121

0

を辞任することを余儀なくされ、それが見えますVCよりも。フリーメモリを再利用する方法によって、さまざまなエラーが発生する可能性がありますが、アクセスが悪い可能性がありますが、メモリ内の他のオブジェクトが再利用された場合、代わりに「認識できないセレクタ」メッセージが表示される可能性があります。

'currentController' varのどこかで 'release'を呼び出すかどうかを確認するか、または 'retain'プロパティとして宣言されているかどうかをチェックし、値をどこかに再割り当てしていないことを確認します現在の参照。 (currentControllerにプロパティ参照で割り当てていないので、直接ivarに割り当てているので、プロパティ参照を使ってどこかで再割り当てを行うと、retainプロパティとして宣言されていれば解放されてしまいます)

+0

currentControllerは、回避策では失敗しました。なぜ私はそれが助けになると思ったのかわかりません、私はちょうどその時点で別のものを試していました。元の投稿を編集して、これらのファイルから現在のコードをすべて追加しました。私は確かに非常に面倒なコードがあり、おそらく最善ではないことをやっているが、やはり私が行くにつれて学習しており、その結果のコードはややこしい – gkres121