如何在不需要时隐藏键盘?

时间:2012-08-15 04:08:12

标签: iphone xcode keyboard

我正在处理的应用程序允许用户使用不同的按钮阅读文本和撰写文本。我现在的问题是,在用于阅读文本的屏幕上,如果用户点击屏幕上用于写入文本的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];
}

}

3 个答案:

答案 0 :(得分:3)

试试这个

[self.textView setEditable:NO];

答案 1 :(得分:1)

当用户点击阅读按钮时,我只需拨打

self.textView.userInteractionEnabled = NO;

如果您希望他们编辑它,请致电

self.textView.userInteractionEnabled = YES;

答案 2 :(得分:0)

嗯,事实证明,我在xib中创建的UITextView一直被遗忘,因为它隐藏在主视图下 - 当我决定创建{{1}时以编程方式我不记得删除另一个,因为我看不到它,但它在那里工作它的邪恶将在整个时间。在从UITextView以外删除整个代码并从xib中删除所有代码之后,我终于想出了这一点。