我有三个屏幕,名为:ViewController,SecondViewController,ThirdViewController
每个屏幕上有两个按钮 在ViewController中:gotoSecondViewController,gotoThirdViewController 在SecondViewController中:gotoViewController,gotoThirdViewController 在ThirdViewController中:gotoViewController,gotoSecondViewController
我需要创建一个通用方法,可以从上面六个方法调用,这六个方法与六个按钮相关联,可以在三个视图之间切换
//
// ViewController.h
// MultiCommonMethod
//
// Created by Hitendra C-Lover on 12/23/13.
// Copyright (c) 2013 Hitendra C-Lover. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface ViewController : UIViewController
@end
//
// ViewController.m
// MultiCommonMethod
//
// Created by Hitendra C-Lover on 12/23/13.
// Copyright (c) 2013 Hitendra C-Lover. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)gotoSecondView:(id)sender {
SecondViewController *screen2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self presentViewController:screen2 animated:YES completion:NO];
[GlobleMethods changeScreen:@"SecondViewController"];
}
- (IBAction)gotoThirdView:(id)sender {
ThirdViewController *screen3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
[self presentViewController:screen3 animated:YES completion:NO];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
****************************
//
// SecondViewController.h
// MultiCommonMethod
//
// Created by Hitendra C-Lover on 12/23/13.
// Copyright (c) 2013 Hitendra C-Lover. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "ViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController : UIViewController
@end
//
// SecondViewController.m
// MultiCommonMethod
//
// Created by Hitendra C-Lover on 12/23/13.
// Copyright (c) 2013 Hitendra C-Lover. All rights reserved.
//
#import "SecondViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (IBAction)gotoFirstView:(id)sender {
ViewController *screen1 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[self presentViewController:screen1 animated:YES completion:NO];
}
- (IBAction)gotoThirdView:(id)sender {
ThirdViewController *screen3 = [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
[self presentViewController:screen3 animated:YES completion:NO];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
*****************
//
// ThirdViewController.h
// MultiCommonMethod
//
// Created by Hitendra C-Lover on 12/23/13.
// Copyright (c) 2013 Hitendra C-Lover. All rights reserved.
//
#import <UIKit/UIKit.h>
#import "ViewController.h"
#import "SecondViewController.h"
#import "GlobleMethods.h"
@interface ThirdViewController : UIViewController
@end
//
// ThirdViewController.m
// MultiCommonMethod
//
// Created by Hitendra C-Lover on 12/23/13.
// Copyright (c) 2013 Hitendra C-Lover. All rights reserved.
//
#import "ThirdViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
- (IBAction)gotoFirstView:(id)sender {
ViewController *screen1 = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[self presentViewController:screen1 animated:YES completion:NO];
}
- (IBAction)gotoSecondView:(id)sender {
SecondViewController *screen2 = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
[self presentViewController:screen2 animated:YES completion:NO];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:1)
您可以创建返回UIViewController的方法,并通过传递屏幕编号,该方法可以返回右视图控制器,然后您可以在视图层次结构中显示它。例如
-(UIViewController*)getViewControllerByNumber:(NSUInteger)screenNo
{
if (screenNo = 2)
{
retuen [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
}
else if (screenNo = 3)
{
return [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
}
else
{
return [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
}
}
你可以这样称呼它:
- (IBAction)gotoThirdView:(id)sender
{
UIViewController *viewc= [self getViewControllerByNumber:3];
// Or you can create class method and call it like that:
// UIViewController *viewc= [ViewManager getViewControllerByNumber:3];
[self presentViewController:viewc animated:YES completion:NO];
}
答案 1 :(得分:-1)
- (IBAction)gotoView:(id)sender{
[GlobleMethods gotoView:sender deleget:self];
}
bind above method to any button's touchUpInside,
buttone title must be screen no like, 1,2,3
//
// GlobleMethods.h
// MultiCommonMethod
//
// Created by Hitendra C-Lover on 12/23/13.
// Copyright (c) 2013 Hitendra C-Lover. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface GlobleMethods : NSObject
+(UIViewController*)getViewControllerByNumber:(NSUInteger)screenNo;
+ (void) gotoView:(id)sender deleget:(id)deleget;
@end
****************************************************
//
// GlobleMethods.m
// MultiCommonMethod
//
// Created by Hitendra C-Lover on 12/23/13.
// Copyright (c) 2013 Hitendra C-Lover. All rights reserved.
//
#import "GlobleMethods.h"
#import "ViewController.h"
#import "SecondViewController.h"
#import "ThirdViewController.h"
@implementation GlobleMethods
+(UIViewController*)getViewControllerByNumber:(NSUInteger)screenNo
{
if (screenNo == 2)
{
return [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
}
else if (screenNo == 3)
{
return [[ThirdViewController alloc] initWithNibName:@"ThirdViewController" bundle:nil];
}
else
{
return [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
}
}
+ (void)gotoView:(id)sender deleget:(id)deleget{
int screenNo = [[sender currentTitle]intValue];
UIViewController *viewc= [self getViewControllerByNumber:screenNo];
[deleget presentViewController:viewc animated:YES completion:NO];
}
@end