Reputation: 373
First ViewController.xib contains 4 views (without UINavigationController in xib).
How to go back to 1 (ViewController.h)?
// Methods
- (IBAction) iBM_GlavView:(id)sender;
- (void) iDM_GlavView;
These methods are not available in GlavView.m if I did not tie the selector button to the First Responder, but they are available in ViewController.m
If you use Interface Builder, then it works, but I do not want to use Interface Builder, and without it, to reach out to ViewController, I do not know.
Thank you for any help!
Here is part of the application code:
--------------- 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)?
}
Upvotes: 1
Views: 212
Reputation: 373
В итоге я сделал подробную и ясную инструкцию. Данный вариант перехода к другому методу выполнен с помощью NSNotificationCenter.
In the end, I made a detailed and clear instructions. This version of the transition to another method was performed with a NSNotificationCenter.
Еще есть вариант с Delegate, но его пока не пробовал.
Another option is a Delegate, but it has not yet tried.
Выполнение метода из любого другого файла
Execution of the method of any other file
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
}
Upvotes: 1
Reputation: 1560
iSavaDevRu, see navigationController methods
this method is for you :)
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];
}
Upvotes: 0