-3

在我的应用程序中,我有标签栏,其中有三个标签按钮。但是在其中一个上,当我第二次按下它时,它会使应用程序崩溃。其他按钮工作正常。

这是 .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 回答