Friday 21 December 2012

In View Controller, Add a Another View Controller (Reusability) in Xcode

Main Page:

.h File

#import <UIKit/UIKit.h>

@interface DetailPage : UIViewController
{
    CGRect PopAnimateStart;
    CGRect PopAnimateEnd;
    UIView *same;

    //Passing Array
    NSMutableArray *mut_some;  
}

-(IBAction)show:(id)sender;

-(IBAction)hide:(id)sender;


@end



.m File

#import "DetailPage.h"
#import "Ad_View.h"
#import "Ad_Detail.h"

@interface DetailPage ()
{
    Ad_View *advertisment;
}

@end

@implementation DetailPage

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title=@"Main Page";
    // Do any additional setup after loading the view from its nib.
}

-(IBAction)show:(id)sender
{
    PopAnimateStart=CGRectMake(0, 480, 320, 62);
    PopAnimateEnd=CGRectMake(0, 354, 320, 62);

    
    advertisment = [[Ad_View alloc]init];
    advertisment.delegate=self;
    
    advertisment.stringTitle=@"Ad Name";
    advertisment.stringContent=@"Content for Ads";

    [self.view addSubview:advertisment.view];
    
    same=advertisment.view;

    same.frame = PopAnimateStart;
    same.alpha = 1.0f;
    [UIView animateWithDuration:0.20 delay:0.1f options:UIViewAnimationCurveEaseOut animations:^{same.frame = PopAnimateEnd;} completion:^(BOOL finished){ }];
}

- (void)cancelBtnClicked:(Ad_View *)Ad_ViewController
{
    [same removeFromSuperview];
     same=nil;
}


//Passing Array

- (void)setString:(NSMutableArray *)array
{
    mut_some=[[NSMutableArray alloc]init];
    mut_some= array;
}

- (void)NavigateBtnClicked:(Ad_View *)Ad_ViewController
{
    Ad_Detail *de=[[Ad_Detail alloc]init];
    [self.navigationController pushViewController:de animated:YES];

    //Passing Array

    /* Ad_Detail *addetail=[[Ad_Detail alloc]init];
    addetail.arr_vendor_det=[[NSMutableArray alloc]init];
    [addetail.arr_vendor_det  addObject:[mut_some objectAtIndex:0]];//0
    [addetail.arr_vendor_det  addObject:[mut_some objectAtIndex:1]];//1
    [addetail.arr_vendor_det  addObject:[mut_some objectAtIndex:2]];//2
    [addetail.arr_vendor_det  addObject:[mut_some objectAtIndex:3]];//3
    [addetail.arr_vendor_det  addObject:[mut_some objectAtIndex:4]];//4
    [addetail.arr_vendor_det  addObject:[mut_some objectAtIndex:5]];//5
    [addetail.arr_vendor_det  addObject:[mut_some objectAtIndex:6]];//6
    [addetail.arr_vendor_det  addObject:[mut_some objectAtIndex:7]];//7
    [addetail.arr_vendor_det  addObject:[mut_some objectAtIndex:8]];//8
    [addetail.arr_vendor_det addObject:[mut_some objectAtIndex:9]];//9
    [addetail.arr_vendor_det addObject:[mut_some objectAtIndex:10]];//10
    [addetail.arr_vendor_det addObject:[mut_some objectAtIndex:11]];//11
    addetail.sid=[mut_some objectAtIndex:12];
    addetail.itemid=[mut_some objectAtIndex:13];
    addetail.uname=username;
    addetail.transfer_res=@"vendor";
    [self.navigationController  pushViewController: addetail animated:YES];*/
}

-(void)viewWillDisappear:(BOOL)animated
{
    [same removeFromSuperview];
    same=nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end





.Xib File



Mappings:

Show: --- >  Show Button





Add Page:

.h File


#import <UIKit/UIKit.h>
@protocol PopupDelegate;

@interface Ad_View : UIViewController
{
    
    IBOutlet UILabel *MyTitle;
    IBOutlet UILabel *Contents;
    
    NSString *stringTitle;
    NSString *stringContent;


    NSMutableArray *mut_some;
    
}
@property (assign, nonatomic) id <PopupDelegate>delegate;

@property(nonatomic,retain)IBOutlet UILabel *MyTitle;
@property(nonatomic,retain)IBOutlet UILabel *Contents;

@property(nonatomic,retain)NSString *stringTitle;
@property(nonatomic,retain)NSString *stringContent;

- (IBAction)Close:(id)sender;
- (IBAction)Navigate:(id)sender;

@end


@protocol PopupDelegate<NSObject>
@optional

- (void)cancelBtnClicked:(Ad_View *)Ad_ViewController;
- (void)NavigateBtnClicked:(Ad_View *)Ad_ViewController;

//passing array
- (void)setString:(NSMutableArray *)array;

@end




.m File


#import "Ad_View.h"

@interface Ad_View ()

@end

@implementation Ad_View

@synthesize stringContent,stringTitle,MyTitle,Contents;
@synthesize delegate;


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    NSLog(stringTitle);
    NSLog(stringContent);
    
    MyTitle.text=stringTitle;
    Contents.text=stringContent;
}


- (IBAction)Close:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cancelBtnClicked:)])
    {
        [self.delegate cancelBtnClicked:self];
    }

}

- (IBAction)Navigate:(id)sender
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(NavigateBtnClicked:)])
    {
        [self.delegate NavigateBtnClicked:self];
        [self.delegate setString:mut_some]; //Passing Array
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end




.Xib File


Mappings:


label --> MyTitle
label --> Contents
Close: -->  "X" button
Navigate: --> Round Rect Button( Invisible- Custom)







Detail Page(only Changed Xib File):

.h File


@interface Ad_Detail : UIViewController

@end



.m File


#import "Ad_Detail.h"

@interface Ad_Detail ()

@end

@implementation Ad_Detail

- (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




.Xib File(Only Designed)





Output:



                   

1)Click the Show Button                  2)Click the "X" button to close the AnotherViewController.
                                                                     3)Press the view to Navigate the Detail Page.









No comments: