IOS Tutorial 2

  • 键盘收起以及弹出对话框
  • ViewController.h里面加入-(IBAction)View_TouchDown:(id)sender;
1
2
3
4
5
6
7
8
9
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property (strong, nonatomic) IBOutlet UITextField *tempText;

-(IBAction)showMessage;
-(IBAction)View_TouchDown:(id)sender;
@end
  • ViewController.m里面加入以下代码
1
2
3
4
- (IBAction)View_TouchDown:(id)sender
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
  • 得到的全部代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

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

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)showMessage  //弹出对话框
{
    UIAlertView *a = [[UIAlertView alloc]
                      initWithTitle:@"I am pb" message:@"hello world" delegate:nil cancelButtonTitle:@"I can do it" otherButtonTitles: nil];
    [a show];
}

- (IBAction)View_TouchDown:(id)sender
{
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}

@end
  • 最后再在storybroad里面用outlet把整个屏幕和View_TouchDown连接起来就可以了

  • Touble shoot

    • 1.出现'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle:
      • 说明self.viewController = [[[ViewController alloc] initWithNibName:@"name" bundle:nil] autorelease];
        • 里面的name写错了
          • 要写成和ViewController一样的名字才行。
IOS
Comments

Comments