如何在DetailView中出现键盘时禁用MasterView

时间:2012-09-19 11:59:24

标签: xcode uisplitviewcontroller uikeyboard disabled-control

我想知道键盘出现在DetailView中是否可能(以及如何),以禁用任何MasterView控件,直到它消失。所有这些都发生在基于拆分视图的应用程序中。

---王子解决方案的更新---

MasterViewController.h

@property (strong, nonatomic) UIView *MasterView;

MasterViewController.m

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

    MasterView=self.view;

    self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
    self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}

DetailViewController.m

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    ((MasterViewController *)self.parentViewController).MasterView.userInteractionEnabled=NO;

    return YES;
}

此代码按原样使用“未知选择器”错误导致应用程序崩溃。 我如何绑定代表;不知道这是不是问题。有什么帮助吗?

2 个答案:

答案 0 :(得分:0)

使用UITextField的委托并绑定委托:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
   MasterView.userInteractionEnabled = NO;

   .......
   return YES;
} 

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

答案 1 :(得分:0)

我找到了一个解决方案!

在MasterView viewDidLoad中

//---registers the notifications for keyboard---
    // to see if keyboard is shown / not shown
    [[NSNotificationCenter defaultCenter]
     addObserver: self
     selector:@selector(keyboardDidShow:)
     name:UIKeyboardDidShowNotification
     object:self.view.window];

    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(keyboardDidHide:)
     name:UIKeyboardDidHideNotification
     object:nil];

然后......:

//----------Handling Keyboard Appearence---
-(void) keyboardDidShow:(NSNotification *) notification {
    [self.tableView setUserInteractionEnabled:NO];
}

//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
    [self.tableView setUserInteractionEnabled:YES];
}

//---before the View window disappear---
-(void) viewWillDisappear:(BOOL)animated {
    //---removes the notifications for keyboard---
    [[NSNotificationCenter defaultCenter]
     removeObserver: self
     name:UIKeyboardWillShowNotification
     object:nil];

    [[NSNotificationCenter defaultCenter]
     removeObserver:self
     name:UIKeyboardWillHideNotification
     object:nil];
}