【问题标题】:How do I keep the keyboard hidden when it's not wanted?不需要时如何隐藏键盘?
【发布时间】:2012-08-11 10:25:31
【问题描述】:

我正在开发的应用程序允许用户使用不同的按钮阅读文本和撰写文本。我现在的问题是,在阅读文本的屏幕上,如果用户在屏幕上用于编写文本的 UITextView 框内点击,则会出现键盘。本例中的 UITextView 为self.textView;我已经将键盘通知和keyboardWillShow方法放在if(self.textView)语句中,然后确保在阅读文本方法的开头调用[self.textView removeFromSuperView]并设置self.textView = nil;,但是当你点击空格时键盘仍然出现self.textView 已设置(以编程方式,顺便说一下,不使用 IB)。

我做错了什么?

编辑:感谢大家的回答,伙计们,但那个该死的键盘仍然不断出现,就像歌曲中的猫一样......这是我的代码。如果可以,请原谅它的长度;如果我做了一些不可靠的事情,我不知道它在哪里,所以我不知道要省略什么。

这里是viewDidLoad

-(void)viewDidLoad {
[super viewDidLoad];
self.textView.editable=NO;
self.textView.userInteractionEnabled = NO;
UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(previousHaiku)];
swipeRight.numberOfTouchesRequired = 1;
swipeRight.direction = UISwipeGestureRecognizerDirectionRight;
[self.view addGestureRecognizer:swipeRight];
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(nextHaiku)];
swipeLeft.numberOfTouchesRequired = 1;
swipeLeft.direction = UISwipeGestureRecognizerDirectionLeft;
[self.view addGestureRecognizer:swipeLeft];
 NSError *error;
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSString *path = [documentsDirectory stringByAppendingPathComponent:@"gayHaiku.plist"];
 NSFileManager *fileManager = [NSFileManager defaultManager];
 if (![fileManager fileExistsAtPath: path])
 {
 NSString *bundle = [[NSBundle mainBundle] pathForResource:@"gayHaiku" ofType:@"plist"];
 [fileManager copyItemAtPath:bundle toPath: path error:&error];
 }
 self.gayHaiku = [[NSMutableArray alloc] initWithContentsOfFile: path];
 [self nextHaiku];
}

这里是nextHaikuviewDidLoad 中调用的最后一个方法——这是读取方法。

-(void)nextHaiku
{
[self.view.layer removeAllAnimations];
[self.bar removeFromSuperview];
self.textToSave=@"";
self.haiku_text.text=@"";
[self.view viewWithTag:1].hidden = NO;
[self.view viewWithTag:3].hidden = NO;
int indexOfHaiku;
NSMutableArray *arrayOfHaikuSeen;
NSString *cat;
if (!self.selectedCategory) cat = @"Derfner";
else cat = self.selectedCategory;
NSArray *filteredArray;
if (cat==@"all")
{
    filteredArray = self.gayHaiku;
    indexOfHaiku = self.indxAll;
    arrayOfHaikuSeen = self.theseAreDoneAll;
}
else
{
    indexOfHaiku = (cat==@"user")?self.indxU:self.indxD;
    arrayOfHaikuSeen = (cat==@"user")?self.theseAreDoneU:self.theseAreDoneD;
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"category == %@", cat];
    filteredArray = [self.gayHaiku filteredArrayUsingPredicate:predicate];
}
int array_tot = [filteredArray count];
int sortingHat;
NSString *txt;
if (array_tot > 0)
{
    if (indexOfHaiku == arrayOfHaikuSeen.count)
    {
        while (true)
        {
            sortingHat = (arc4random() % array_tot);
            if (![arrayOfHaikuSeen containsObject:[filteredArray objectAtIndex:sortingHat]]) break;
        }
        txt = [[filteredArray objectAtIndex:sortingHat] valueForKey:@"quote"];
        if (!arrayOfHaikuSeen || arrayOfHaikuSeen.count==array_tot)
        {
            arrayOfHaikuSeen = [[NSMutableArray alloc] init];
        }
        [arrayOfHaikuSeen addObject:[filteredArray objectAtIndex:sortingHat]];
        indexOfHaiku = arrayOfHaikuSeen.count;
        if (arrayOfHaikuSeen.count==filteredArray.count)
        {
            [arrayOfHaikuSeen removeAllObjects];
            indexOfHaiku=0;
        }
    }
    else 
    {
        txt = [[arrayOfHaikuSeen objectAtIndex:indexOfHaiku] valueForKey:@"quote"];
        indexOfHaiku += 1;
    }
}
//Need to test to make sure it starts over once all 110 haiku have been seen.

CGSize dimensions = CGSizeMake(320, 400);
CGSize xySize = [txt sizeWithFont:[UIFont fontWithName:@"Helvetica" size:14.0] constrainedToSize:dimensions lineBreakMode:0];
self.haiku_text = [[UITextView alloc] initWithFrame:CGRectMake((320/2)-(xySize.width/2),200,320,200)];
self.haiku_text.font = [UIFont fontWithName:@"Helvetica Neue" size:14];
self.haiku_text.backgroundColor = [UIColor clearColor];
self.haiku_text.text=txt;
CATransition *transition = [CATransition animation];
transition.duration = 0.25;
transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
transition.type = kCATransitionPush;
transition.subtype =kCATransitionFromRight;
transition.delegate = self;
[self.view.layer addAnimation:transition forKey:nil];
[self.view addSubview:self.haiku_text];

if (cat==@"user")
{
    self.theseAreDoneU = arrayOfHaikuSeen;
    self.indxU = indexOfHaiku;
}
else if (cat==@"all")
{
    self.theseAreDoneAll = arrayOfHaikuSeen;
    self.indxAll = indexOfHaiku;
}
else 
{
    self.theseAreDoneD = arrayOfHaikuSeen;
    self.indxD = indexOfHaiku;
}
}

这是写法。

-(void)userWritesHaiku

//Set up the screen.
[self clearScreen];
self.textView = [[UITextView alloc] initWithFrame:CGRectMake(20, 60, 280, 150)];
self.textView.delegate = self;
self.textView.returnKeyType = UIReturnKeyDefault;
self.textView.keyboardType = UIKeyboardTypeDefault;
self.textView.font = [UIFont fontWithName:@"Helvetica Neue" size:14];
self.textView.scrollEnabled = YES;
self.textView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
self.textView.userInteractionEnabled = YES;
self.textView.editable=YES;
self.textView.backgroundColor = [UIColor colorWithRed:217 green:147 blue:182 alpha:.5];
[self.view addSubview: self.textView];

[self loadNavBar:@"Compose"];
[self addLeftButton:@"Instructions" callingMethod:@"haikuInstructions"];

//If you've added text before calling haikuInstructions, when you return from haikuInstructions the textView window with the different background color AND the keyboard.
[self addRightButton:@"Done" callingMethod:@"userFinishedWritingHaiku"];
self.titulus.hidesBackButton=YES;
[self seeNavBar];

//Create and add the space for user to write.
[self createSpaceToWrite];
if (self.textToSave!=@"")
{
    self.textView.text = self.textToSave;
}
[self.view addSubview:self.textView];
[self.textView becomeFirstResponder];

//Keyboard notifications.

if (self.textView)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

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

}

【问题讨论】:

  • 你能描述更多关于按钮的信息

标签: iphone xcode keyboard


【解决方案1】:

试试这个

[self.textView setEditable:NO];

【讨论】:

  • Drat——这似乎也没有任何作用。我已经在对问题的编辑中发布了上面的代码——也许我做了一些奇怪的事情并且在没有意识到的情况下搞砸了(这肯定是第一次......)。
【解决方案2】:

当用户点击阅读按钮时,我会调用

self.textView.userInteractionEnabled = NO;

当你想让他们编辑它时调用

self.textView.userInteractionEnabled = YES;

【讨论】:

  • 抱歉,我查看了文档及其要为该文档设置的可编辑属性。 self.textView.editable = 否;例如。这是文档的链接:developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/…
  • 不幸的是,这似乎也不起作用。我已经编辑了我的问题,以防万一我在其他地方做了一些奇怪的事情......
  • @Joel Derfner 当您第一次运行该应用程序时,屏幕上是否存在先前的条目?如果是,当时是否可以编辑?
  • 一开始屏幕上没有条目——只有一个可编辑的文本视图和键盘,尽管我可以让键盘仅在用户点击文本视图后出现.我开始怀疑一个奇怪的错误,它只是非常非常有效地隐藏起来......
【解决方案3】:

好吧,事实证明,我在 xib 中创建了一个 UITextView 并忘记了,因为它隐藏在主视图下——当我决定以编程方式创建 UITextView 时,我没有不记得删除另一个,因为我看不到它,但它一直在那里运作着它的邪恶意志。在注释掉除 [super viewDidLoad] 之外的整个代码并从 xib 中删除所有内容后,我终于明白了这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-12
    • 2023-01-09
    • 1970-01-01
    • 2019-10-22
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多