如何从AppDelegate访问按钮

时间:2013-03-06 07:23:26

标签: iphone xcode

我在Appdelegate中查看了一个视图,并在其上添加了6个uibutton ..并在每个课程中添加此视图  self.navigationItem.titleView = objAppDelegate.viewForBtn;

当从每个分支回来时,这个视图没有出现。这背后的问题是什么? 和代码在这里..

在Appdelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

  self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease];

navcontrller=[[UINavigationController alloc]initWithRootViewController: self.viewController];

viewForBtn=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 320,44)];
viewForBtn.backgroundColor=[UIColor clearColor];

UIButton *loginbtn=[[UIButton alloc]initWithFrame:CGRectMake(160, 7, 45, 25)];
loginbtn.backgroundColor=[UIColor clearColor];
[loginbtn setTitle:@"Login" forState:UIControlStateNormal];
[loginbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[loginbtn.titleLabel setFont:[UIFont fontWithName:@"Helvetica Bold " size:13]];
[loginbtn addTarget:self action:@selector(loginbtnpressed) forControlEvents:UIControlEventTouchUpInside];

UIButton *sharebtn=[[UIButton alloc]initWithFrame:CGRectMake(210, 7, 25, 30)];
[sharebtn setImage:[UIImage imageNamed:@"sharing11.png"] forState:UIControlStateNormal];
[sharebtn addTarget:self action:@selector(sharebtnpressed) forControlEvents:UIControlEventTouchUpInside];

UIButton *likebtn=[[UIButton alloc]initWithFrame:CGRectMake(240, 0, 25, 44)];
[likebtn setImage:[UIImage imageNamed:@"LikeIt1.png"] forState:UIControlStateNormal];
[likebtn addTarget:self action:@selector(likebtnpressed) forControlEvents:UIControlEventTouchUpInside];

UIButton *optionbtn=[[UIButton alloc]initWithFrame:CGRectMake(270, 0, 25, 44)];
[optionbtn setImage:[UIImage imageNamed:@"detail.png"] forState:UIControlStateNormal];
[optionbtn addTarget:self action:@selector(optionbtnpressed) forControlEvents:UIControlEventTouchUpInside];


UIButton *backbtn=[[UIButton alloc]initWithFrame:CGRectMake(20, 7, 45, 30)];
backbtn.backgroundColor=[UIColor clearColor];
[backbtn setTitle:@"Back" forState:UIControlStateNormal];
[backbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[backbtn.titleLabel setFont:[UIFont fontWithName:@"Helvetica Bold" size:14]];
[backbtn addTarget:self action:@selector(backbtnpressed)  forControlEvents:UIControlEventTouchUpInside];

UIButton *homebtn=[[UIButton alloc]initWithFrame:CGRectMake(90, 0, 25, 44)];
[homebtn setImage:[UIImage imageNamed:@"uparrow.png"] forState:UIControlStateNormal];
[homebtn addTarget:self action:@selector(homebtnpressed) forControlEvents:UIControlEventTouchUpInside];


[viewForBtn addSubview:loginbtn];
[viewForBtn addSubview:sharebtn];
[viewForBtn addSubview:likebtn];
[viewForBtn addSubview:optionbtn];
[viewForBtn addSubview:backbtn];
[viewForBtn addSubview:homebtn];

self.window.rootViewController = navcontrller;
[self.window makeKeyAndVisible];
return YES;

}

-(void)loginbtnpressed

{

      signIn=[[SignInView alloc]init];

    [ self.navcontrller pushViewController:signIn animated:YES];

}

 -(void)sharebtnpressed

{


   NSLog(@"Share button pressed from button class");


}


-(void)likebtnpressed

{

 NSLog(@"Like button pressed from button class");


}


-(void)optionbtnpressed

{

 NSLog(@"Options button pressed from button class");

}



-(void)backbtnpressed

{

    [self.navcontrller popViewControllerAnimated: YES];

}

-(void)homebtnpressed

{

    [self.navcontrller popToRootViewControllerAnimated: YES];

}



in ViewController.m

- (void)viewDidLoad

{

   [super viewDidLoad];

       objAppDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];

    self.navigationItem.titleView.backgroundColor=[UIColor clearColor];
        self.navigationItem.titleView = objAppDelegate.viewForBtn;
}


-(IBAction)btnClk:(id)sender

{

    SecondViewController *objSecondViewController=[[SecondViewController alloc]init];
    [self.navigationController pushViewController:objSecondViewController animated:YES];

}


-(void)viewWillAppear:(BOOL)animated

{

    self.navigationItem.titleView.backgroundColor=[UIColor clearColor];
    self.navigationItem.titleView = objAppDelegate.viewForBtn;


}


in  SecondViewController.m

   - (void)viewDidLoad

{

    [super viewDidLoad];

    objAppDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    self.navigationItem.hidesBackButton=YES;

}

-(IBAction)btn1Clk:(id)sender

{

    objThirdViewController=[[ThirdViewController alloc]init];
    [self.navigationController pushViewController:objThirdViewController animated:YES];

}

-(void)viewDidAppear:(BOOL)animated

{

    self.navigationItem.titleView.backgroundColor=[UIColor clearColor];
    self.navigationItem.titleView = objAppDelegate.viewForBtn;

}

in ThirdViewController.m

    - (void)viewDidLoad
{

    [super viewDidLoad];

    objAppDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
    self.navigationItem.hidesBackButton=YES;

}

-(void)viewDidAppear:(BOOL)animated

{

    self.navigationItem.titleView.backgroundColor=[UIColor clearColor];
    self.navigationItem.titleView = objAppDelegate.viewForBtn;

}

0 个答案:

没有答案