-2

我正在尝试学习如何获得一个按钮(在我的情况下是一个“取消”按钮)来引导我回到它所在的上一页。我的上一页上有一个按钮,可将我引导至此页面,我希望此按钮将我带回上一页并丢弃我在该页面上写的任何内容。我的第一页上的按钮通过模式连接将视图推送到此页面。我知道只为取消按钮使用简单的模式连接来返回我的原始页面是有意义的,但我希望有一种更优雅的方式来做到这一点。我敢肯定,创建自定义视图是一个好主意,但我真的不知道该怎么做......哈哈,但任何建议都将不胜感激!

#import "AddEventViewController.h"
#import <QuartzCore/QuartzCore.h>

@interface AddEventViewController ()
@property (weak, nonatomic) IBOutlet UITextField *textField1;
@property (weak, nonatomic) IBOutlet UITextField *textField2;
@property (weak, nonatomic) IBOutlet UITextField *textField3;
@property (strong, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (weak, nonatomic) IBOutlet UINavigationBar *addEventTitleBar;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *cancelButton;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *saveButton;
@property (weak, nonatomic) IBOutlet UITextView *myTextView;
- (IBAction)textFieldReturn:(id)sender;

@end

@implementation AddEventViewController

@synthesize textField1, textField2, textField3, myTextView;



- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (IBAction)textFieldReturn:(id)sender;
{
    [sender resignFirstResponder];
}

- (void)viewDidLoad
{      // Do any additional setup after loading the view.
    [super viewDidLoad];
    self.textField1.delegate = self;
    textField1.delegate = self;
    self.textField2.delegate = self;
    textField2.delegate = self;
    self.textField3.delegate = self;
    textField3.delegate = self;

    [myTextView.layer setCornerRadius:10.0f];
    [myTextView.layer setBorderColor:[UIColor lightGrayColor].CGColor];
    [myTextView.layer setBorderWidth:1.5f];
    [myTextView.layer setShadowColor:[UIColor blackColor].CGColor];
    [myTextView.layer setShadowOpacity:0.002f];
    [myTextView.layer setShadowRadius:3.0f];
    [myTextView.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
}

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

- (void)touchesBegan: (NSSet *) touches withEvent: (UIEvent *)event
{
    if (textField1)
    {
        if ([textField1 canResignFirstResponder]) [textField1 resignFirstResponder];
    }
    [super touchesBegan: touches withEvent: event];

    if (textField2)
    {
        if ([textField2 canResignFirstResponder]) [textField2 resignFirstResponder];
    }
    [super touchesBegan: touches withEvent: event];

    if (textField3)
    {
        if ([textField3 canResignFirstResponder]) [textField3 resignFirstResponder];
    }
    [super touchesBegan: touches withEvent: event];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField == textField1)
    {
        [textField1 resignFirstResponder];
    }
    else if (textField == textField2)
    {
        [textField2 resignFirstResponder];
    }
    else if (textField == textField3)
    {
        [textField3 resignFirstResponder];
    }
    return YES;
}

-(void)addCancelButton
{
    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"style:UIBarButtonSystemItemAction target:self action:@selector(cancel:)];

    self.navigationItem.leftBarButtonItem = cancelButton;
}

-(void)cancel:(id)sender
{
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];

    [self.navigationController popViewControllerAnimated:NO];
}

@end

修复了问题!

4

1 回答 1

2

你可以这样解决它:

- (void)addCancelButton{

    UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:@"Cancel"
                                                                     style:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];


    self.navigationItem.leftBarButtonItem = cancelButton;

}



- (void)cancel:(id)sender{
    //If presented
    [self.navigationController dismissViewControllerAnimated:YES completion:nil];

    //If pushed
    [self.navigationController popViewControllerAnimated:NO];
    }

AddCancelButton 中的代码可以移动到 viewDidLoad 或适当的地方。您可能希望根据视图的添加方式(推送、呈现等)来编辑视图的移除方式。

于 2012-12-18T21:00:28.130 回答