如何返回第一个xib视图

时间:2012-01-20 16:05:19

标签: iphone objective-c

第一个ViewController.xib包含4个视图(在xib中没有UINavigationController)。

  1. “ViewController.h” - 在这里......
  2. “GlavView.h” - 来到这里......
  3. 如何返回1(ViewController.h)?

    //方法

    - (IBAction) iBM_GlavView:(id)sender;
    - (void) iDM_GlavView;
    

    如果我没有将选择器按钮绑定到First Responder,这些方法在GlavView.m中不可用,但它们在ViewController.m中可用

    如果您使用Interface Builder,那么它可以工作,但是我不想使用Interface Builder,如果没有它,可以使用ViewController,我不知道。

    感谢您的帮助!

    以下是应用程序代码的一部分:

    --------------- AppDelegate.h

    //
    //  AppDelegate.h
    //
    //
    
    
    #import <UIKit/UIKit.h>
    
    @class ViewController;
    
    @interface AppDelegate : NSObject <UIApplicationDelegate> //UIResponder <UIApplicationDelegate>
    
    @property (strong, nonatomic) UIWindow *window;
    
    @property (strong, nonatomic) ViewController *viewController;
    
    @end
    

    --------------- ViewController.h

    //
    //  ViewController.h
    //
    
    #import <UIKit/UIKit.h>
    
    @interface ViewController : UIViewController{
        UINavigationController *navigationController;
    }
    
    
    @property (retain, nonatomic) IBOutlet UINavigationController *navigationController;
    
    
    - (IBAction) iBM_GlavView:(id)sender;
    - (void) iDM_GlavView;
    
    @end
    

    --------------- ViewController.m

    //
    //  ViewController.m
    //  SeifNote
    //
    //  Created by Saveliy Severniy on 19.01.12.
    //  Copyright (c) 2012 Saveliy Severniy. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "GlavView.h"
    
    @implementation ViewController
    
    @synthesize navigationController;
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        if( isLogin )
        {
            // Ok
            [self iDM_GlavView];
        }
        else
        {
    
            [self.view addSubview:self.viewEnterPass];
        }
    }
    
    - (IBAction)iBM_GlavView:(id)sender{
    
        if (self.navigationController == nil) {
    
            //navigationController is declared as: UINavigationController * navigationController;
            navigationController = [[UINavigationController alloc] init];
    
            GlavView *myOptions = [[GlavView alloc] initWithNibName:@"GlavView" bundle:nil];
    
            [navigationController pushViewController:myOptions animated:NO];
            [myOptions release];
    
        }
    
        [self.view addSubview:navigationController.view]; // From this point begins the work UINavigationController in a new view
    
    }
    
    
    
    
    - (void) iDM_GlavView {
    
        if (self.navigationController == nil) {
    
            // navigationController is declared as: UINavigationController *optionsRootController;
            navigationController = [[UINavigationController alloc] init];
    
            GlavView *myOptions = [[GlavView alloc] initWithNibName:@"GlavView" bundle:nil];
    
            [navigationController pushViewController:myOptions animated:NO];
            [myOptions release];
    
        }
    
        [self.view addSubview:navigationController.view];
    }
    

    --------------- GlavView.h

    //
    //  GlavView.h
    //
    
    #import <UIKit/UIKit.h>
    
    @interface GlavView : UITableViewController {
        UIToolbar *toolbar;
        IBOutlet UIBarButtonItem *addButtonItem;
    }
    
    @property (nonatomic, retain) UIBarButtonItem* addButtonItem;
    
    @end
    

    --------------- GlavView.m

    //
    //  GlavView.m
    //
    
    #import "GlavView.h"
    
    @implementation GlavView
    
    @synthesize addButtonItem;
    
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
        // Do any additional setup after loading the view from its nib.
    
        //  is declared as: UIBarButtonItem *addButtonItem;
        self.navigationItem.rightBarButtonItem = self.addButtonItem;
    }
    
    
    - (void)viewWillAppear:(BOOL)animated {
    
        [super viewWillAppear:animated];
    
        //Initialize the toolbar
        toolbar = [[UIToolbar alloc] init];
        toolbar.barStyle = UIBarStyleDefault;
    
        //Set the toolbar to fit the width of the app.
        [toolbar sizeToFit];
    
        //Caclulate the height of the toolbar
        CGFloat toolbarHeight = [toolbar frame].size.height;
    
        //Get the bounds of the parent view
        CGRect rootViewBounds = self.parentViewController.view.bounds;
    
        //Get the height of the parent view.
        CGFloat rootViewHeight = CGRectGetHeight(rootViewBounds);
    
        //Get the width of the parent view,
        CGFloat rootViewWidth = CGRectGetWidth(rootViewBounds);
    
        //Create a rectangle for the toolbar
        CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
    
        //Reposition and resize the receiver
        [toolbar setFrame:rectArea];
    
    
        //Create a button
        UIBarButtonItem *testButton = [[UIBarButtonItem alloc] 
                                       initWithTitle:@"Del" style:UIBarButtonItemStyleBordered target:self action:@selector(test_clicked:)];
    
        [toolbar setItems:[NSArray arrayWithObjects:testButton,nil]];
    
        [testButton release];
    
        //Add the toolbar as a subview to the navigation controller.
        [self.navigationController.view addSubview:toolbar];
    
        //Reload the table view
        [self.tableView reloadData];
    }
    
    
    - (void) test_clicked:(id)sender {
    
        // iBM_GlavView is declared in ViewController.h
    
        [??? iBM_GlavView]; // How to get out of here (GlavView) and return (ViewController)?
    }
    

2 个答案:

答案 0 :(得分:1)

Витогеясделалподробнуюияснуюинструкцию。 ДанныйвариантпереходакдругомуметодувыполненспомощьюNSNotificationCenter。

最后,我做了详细而明确的指示。 此版本的转换为另一种方法是使用NSNotificationCenter执行的。

---

ЕщеестьвариантсSeamate,ноегопоканепробовал。

另一个选项是代表,但尚未尝试过。

---

Выполнениеметодаизлюбогодругогофайла

执行任何其他文件的方法

<强> NSNotificationCenter

//##############################################
// (From:) Откуда должны прийти (buttonClick)
//##############################################

// 
// Файл start.m

- (void) start:(id)sender {

    NSDictionary *dataInfo = ?data?;    // may be nil

    [[NSNotificationCenter defaultCenter] postNotificationName:@"Info" object:nil userInfo:dataInfo];

    // Or without data

    [[NSNotificationCenter defaultCenter] postNotificationName:@"Info" object:nil];
}


//##########################################
// (To:) Куда должны прийти и выполнить метод
//##########################################

// 
// В файле end.h:

- (void) end: (NSNotification *)notification;

// В файле end.m:

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

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(end:) name:@"Info" object:nil];
}

- (void) end: (NSNotification *)notification {

    // The End
}

答案 1 :(得分:0)

iSavaDevRu,见navigationController methods

this method适合您:)

NSNotificationCenterудобнаяштука смотриделайтак

//добовляем из кода метод к нажатие кнопки

    [button addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
-(void)buttonPressed {
[[NSNotificationCenter defaultCenter] postNotificationName:@"BUTTONPRESSED" object:nil];
}

//а потом добавляем в нашем контороллере чтоб вызывался наш метод при вывидение этой нотификаций
-(void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(IBM_DelPass:) name:@"BUTTONPRESSED" object:nil];
}