2012-12-18 3 views
-2

나는 이전 페이지로 되돌아 가도록 단추 (내 경우 '취소'단추)를 얻는 방법을 배우려고합니다. 이전 페이지에서이 페이지로 이동하게하는 버튼이 있는데,이 버튼을 사용하면 이전 페이지로 돌아가서 페이지에 쓰여진 내용을 삭제할 수 있습니다. 내 첫 페이지의 내 버튼은 모달 연결을 통해보기를이 페이지로 푸시합니다. Cancel 버튼에 대한 간단한 모달 연결을 사용하여 원래 페이지로 돌아가는 것이 합리적이라고 생각하지만이 작업을 수행하는보다 우아한 방법을 기대했습니다. 커스텀 뷰를 만드는 것은 좋은 생각입니다.하지만 어떻게 해야할지 잘 모르겠습니다. 하하. 그러나 어떤 제안이라도 대단히 감사하겠습니다!iOS의 버튼 사용하기

#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 

문제가 해결되었습니다.

+0

plse는 더 많은 정보를 제공하며 네비게이션 컨트롤러가 있습니까? 보기 등을 어떻게 밀어 넣을까요? –

+0

예. 탭 응용 프로그램을 사용하고 있습니다. 내 첫 번째 탭 상단에 탐색 모음이 있습니다. 오른쪽 상단의 바 버튼 항목은이 페이지로 안내합니다. 그리고이 페이지에는 탐색 바가 있습니다. 왼쪽 상단의 '취소'버튼을 사용하여 원래의 첫 번째 탭보기 컨트롤러로 돌아가고 싶습니다. 취소 버튼을 클릭하면 페이지에서 수행 한 모든 내용이 삭제됩니다. @SpaceDust – trludt

답변

2

당신은 이런 식으로 해결할 수 : AddCancelButton의 코드가있는 viewDidLoad로 이동하거나 어딘가에 해당 될 수

- (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]; 
    } 

. 그리고 추가 된 방법 (푸시, 프리젠 테이션 등)에 따라보기가 제거되는 방식을 편집 할 수 있습니다.

+0

'style : UIBarButtonSystemItemAction'? 확실합니까? 제발 잘못 대답을 게시하지 마십시오 ... –

+0

Ooops, 당신은 절대 옳다. – johan

+0

나는 내 .m 파일에 그 코드를 넣고 작동하지 않았다. 그래서 (void) addCancelButton 부분을 내 viewDidLoad 부분에 두어 보았는데, 그 중 하나가 작동하지 않았다 ... – trludt

관련 문제