-3

私のアプリにはタブバーがあり、その中に3つのタブボタンがあります。しかし、そのうちの1つで2回目に押すと、アプリがクラッシュします。他のボタンは正常に動作しています。

これは .m ファイル コードです。

-(void)viewWillAppear:(BOOL)animated
{
    scrollView.contentSize = CGSizeMake(320, 500);
    [super viewWillAppear:YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Contact Us", @"Contact Us");
        self.tabBarItem.image = [UIImage imageNamed:@"contactTab.png"];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];

    scrollView.contentSize = CGSizeMake(320, 500);
    scrollView.scrollEnabled = YES;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

-(BOOL) textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    [textView becomeFirstResponder];
    return YES;
 }

- (void)keyboardWasShown:(NSNotification *)notification
{

    // Step 1: Get the size of the keyboard.
    CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;


    // Step 2: Adjust the bottom content inset of your scroll view by the keyboard height.
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0);
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;


    // Step 3: Scroll the target text field into view.
    CGRect aRect = self.view.frame;
    aRect.size.height -= keyboardSize.height;
    if (!CGRectContainsPoint(aRect, self.activeTextField.frame.origin) )
    {
        CGPoint scrollPoint = CGPointMake(0.0, self.activeTextField.frame.origin.y - (keyboardSize.height-15));
        [scrollView setContentOffset:scrollPoint animated:YES];
    }
}

- (void) keyboardWillHide:(NSNotification *)notification
{
    UIEdgeInsets contentInsets = UIEdgeInsetsZero;
    scrollView.contentInset = contentInsets;
    scrollView.scrollIndicatorInsets = contentInsets;
}

// Set activeTextField to the current active textfield
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.activeTextField = textField;
}

// Set activeTextField to nil
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.activeTextField = nil;
}

// Dismiss the keyboard
- (IBAction)dismissKeyboard:(id)sender
{
    [self.activeTextField resignFirstResponder];
}

では、どのコードが私のアプリをクラッシュさせているのでしょうか?

編集:

AppDelegate.m

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
 {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

tabBarController = [[UITabBarController alloc] init];

//Adding Search,Nearby,Map,AboutUs,Favorites Tabs to tabBarController
HomeViewController * homeViewController = [[HomeViewController alloc] init];
UINavigationController *homeNav = [[UINavigationController alloc] initWithRootViewController:homeViewController];

ContactUsFormViewController *contactUsFormViewController = [[ContactUsFormViewController alloc] init];
UINavigationController *contactNav = [[UINavigationController alloc] initWithRootViewController:contactUsFormViewController];

BookingFormViewController *bookingFormViewController = [[BookingFormViewController alloc] init];
UINavigationController *bookingNav = [[UINavigationController alloc] initWithRootViewController:bookingFormViewController];

NSArray* controllers = [NSArray arrayWithObjects:homeNav,contactNav,bookingNav, nil];

tabBarController.viewControllers = controllers;

[self.window setRootViewController:tabBarController];

sleep(2);

[self.window makeKeyAndVisible];
return YES;

}

クラッシュのログ

ありがとう。

4

1 に答える 1

1
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.


}


-(void)viewWillAppear:(BOOL)animated
{
    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];

    scrollView.contentSize = CGSizeMake(320, 500);
    scrollView.scrollEnabled = YES;

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWasShown:)
                                                 name:UIKeyboardDidShowNotification
                                               object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];

    scrollView.contentSize = CGSizeMake(320, 500);
    [super viewWillAppear:YES];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Contact Us", @"Contact Us");
        self.tabBarItem.image = [UIImage imageNamed:@"contactTab.png"];
    }
    return self;
}
于 2013-06-27T04:45:31.710 に答える