[iOS프로그래밍] Navigation Bar 에 Button 붙이기


iOS/아이폰 프로그래밍 2012.05.24 11:10



Objective-C 관련 자료는 정말정말 네이버보다는 구글링이 짱인 듯 싶습니다. 대부분 찾는 내용들이 http://stackoverflow.com/ 에 있더군요~ 필요한 것들은 개인적으로 Evernote 에 저장해놓고 검색해서 쓰는데, 포스팅도 같이 해봅니다. 


 (대부분 Evernote 에 Copy 본을 올리는 부분은 StackOver 의 질문과 대답 부분을 발췌해서 포스팅할 생각입니다.)


I am making a simple app to display drink details, and now I am trying to add a view that allows the user to input their own drink. I already created a view to display the details, and now I am just passing the view into another controller to make the add drink view. Problem is, when I try to add a "cancel" and "save" button, it doesn't appear, although the code complies without any errors. I have attached code as reference.

This is the code that makes the new view, when the add button is pressed. (I made an add button that works, and it pulls up the nav bar)

-(IBAction)addButtonPressed:(id)sender {

    AddDrinkViewController*addViewController =[self.storyboard instantiateViewControllerWithIdentifier:@"DetailSecond"];
    UINavigationController*addNavController =[[UINavigationController alloc] initWithRootViewController:addViewController];

    [self presentModalViewController:addNavController animated:YES];



    NSLog(@"Add button pressed!");

This is the code from the addviewcontroller implementation file:

-(void)viewDidLoad
{
    [super viewDidLoad];

    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];

    self.navigationItem.rightBarButtonItem =[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(save:)];

}

-(IBAction)save:(id)sender {

    NSLog(@"Save Pressed");
    [self dismissModalViewControllerAnimated:YES];

}

-(IBAction)cancel:(id)sender{

    NSLog(@"Cancel Pressed");
    [self dismissModalViewControllerAnimated:YES];
}

I have imported the header from the addview into the root controller, so I don't think that is the problem, do any of you guys see anything that's wrong?`


feedback

 

My advice to you is to create a template for the view before you run through any code in the XIB file of your app. Rather than trying to set each button after allocating a brand new view, setting a new one in the XIB before-hand allows you to link each element with the app and make sure it looks just right before you debug.

Simply go into your "[Your-App-Name]viewController.xib" and drag a view from the objects library to the pane on the left. From here add each of your elements and position them where you want on the view. Now in the "[Your-App-Name]viewController.h" file, add IBOutlets for each element that you need to change, and add IBActions for each of the buttons. Also create an IBOutlet for the new view.

IBOutletUIView* addDrinkView;

Back in the XIB file, use files owner to link each outlet to each element and each method to each button. Make sure you link the IBOutlet

Now in your "[Your-App-Name]viewController.m" file, you can define each button method and all you need to do to access the new view and dismiss it are the following:

-(IBAction)openAddView
{
     [self setView:addDrinkView];
}
-(IBAction)saveButtonPressed
{
     [self setView:view];
     //save code goes here
}
-(IBAction)cancelButtonPressed
{
     [self setView:view];
     //cancel code goes here
}

This should be much easier than trying to position everything in code.

Hope this helps!








저작자 표시 비영리 변경 금지
신고

WRITTEN BY
ShakeJ

0 ,