当不需要时,我如何保持键盘隐藏?
我正在开发的应用程序允许用户使用不同的按钮阅读文本和撰写文本。我现在的问题是,在阅读文本的屏幕上,如果用户点击屏幕上用于写入文本的UITextView框,键盘会出现。在这种情况下UITextView是self.textView
;我已经把键盘通知和内部if(self.textView)
报表keyboardWillShow方法,然后确保调用[self.textView removeFromSuperView]
并在阅读文本的方法开始设置self.textView = nil;
,但是当你点击其中self.textView
设置的空间(编程键盘仍然出现,顺便说一句,不使用IB)。当不需要时,我如何保持键盘隐藏?
我在做什么错?
编辑:感谢您的答案,家伙和女孩,但仍然那darn键盘不断回来,就像歌曲中的猫....这是我的代码。请原谅它的长度,如果可以的话;如果我做了一些不知情的事情,我不知道它在哪里,所以我不知道该怎么办。
这里的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];
}
这里的nextHaiku
,堪称viewDidLoad
的最后一个方法 - 这是阅读方法。
-(void)nextHaiku
{
[self.view.layer removeAllAnimations];
[self.bar removeFromSuperview];
[email protected]"";
[email protected]"";
[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 ([email protected]"all")
{
filteredArray = self.gayHaiku;
indexOfHaiku = self.indxAll;
arrayOfHaikuSeen = self.theseAreDoneAll;
}
else
{
indexOfHaiku = ([email protected]"user")?self.indxU:self.indxD;
arrayOfHaikuSeen = ([email protected]"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 ([email protected]"user")
{
self.theseAreDoneU = arrayOfHaikuSeen;
self.indxU = indexOfHaiku;
}
else if ([email protected]"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 ([email protected]"")
{
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];
}
}
那么,事实证明,整个过程中有一个UITextView
我在xib中创建并忘记了,因为它隐藏在主视图下 - 当我决定以编程方式创建UITextView
时,我不记得删除另一个,因为我看不到它,但是它一直在那里工作它的邪恶意志。除了[super viewDidLoad]
和删除xib中的所有内容之外,我终于明白了这一点。
当用户点击阅读按钮,我只想叫
self.textView.userInteractionEnabled = NO;
,当你希望他们编辑致电
self.textView.userInteractionEnabled = YES;
可惜的是,似乎没有什么改变,当我尝试这样做。 – 2012-08-15 04:41:10
对不起,我查看了文档及其想要为其设置的可编辑属性。 self.textView.editable = NO;例如。继承人的链接文档:http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITextView_Class/Reference/UITextView.html – Jamie 2012-08-15 05:06:50
不幸的是,这似乎并没有这样的伎俩,无论是。我编辑了我的问题,以防万一我在其他地方做了些什么...... – 2012-08-16 15:23:03
试试这个
[self.textView setEditable:NO];
Drat - 这似乎也没有做任何事情。我已经在上面发布了我的代码,以便对这个问题进行编辑 - 也许我做了一些奇怪的事情,并且没有意识到它(这当然是第一个......)。 – 2012-08-15 05:32:28
+1为此邀请我:] – java 2013-07-31 04:40:52
你能描述更多关于按钮 – Hiren 2012-08-15 04:37:03