Как вернуться к первому просмотру XIB - PullRequest
1 голос
/ 20 января 2012

First ViewController.xib содержит 4 представления (без UINavigationController в xib).

  1. "ViewController.h" - здесь ...
  2. "GlavView.h" - полученоздесь ...

Как вернуться к 1 (ViewController.h)?

// Методы

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

Эти методы недоступны в GlavViewЕсли я не привязал кнопку выбора к первому ответчику, но они доступны в 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.ч

//
//  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 ]

1 голос
/ 21 января 2012

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

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

---

Еще есть вариант с делегатом, но его пока не пробовал.

Другой вариант -a Делегат, но он еще не пытался.

---

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

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

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
}
0 голосов
/ 20 января 2012

iSavaDevRu, см. методы навигационного контроллера

этот метод для вас:)

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];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...