user1120133
user1120133

Reputation: 3234

dismissing modalview is not working

i m presenting modalviewcontroller without using delegate protocol. But want to dismiss modalviewcontroller using delegate protocol.

Basically i m pushing modalviewcontroller like this

- (void)displayModalViewaction: (id) sender 
 {
self.view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.viewController = [[Infoviewcontroller alloc] init];

[self.view setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];


UINavigationController *navigationController=[[UINavigationController alloc] init];

navigationController.navigationBar.tintColor = [UIColor brownColor]; 

[navigationController pushViewController:_viewController animated:YES];

[self.view addSubview:navigationController.view];

[_viewController release];

[navigationController release];

}

For dismissing modalview doing this

In the ModalViewController.h delegated protocol and method

 @protocol ModalViewDelegate <NSObject>

 -(void) dismissModalView:(UIViewController *) viewController;

 @end

 @interface Infoviewcontroller : UIViewController <ModalViewDelegate>

 {
    id<ModalViewDelegate> dismissDelegate;
  }

@property (nonatomic, retain) id<ModalViewDelegate> dismissDelegate;

@end

In modalviewcontroller. m file

@synthesize dismissDelegate;

-(void) dismissModalView:(UIViewController *) viewController;
{

   [self dismissModalViewControllerAnimated:YES];

 }
  @end

   -(void) dismissView: (id)sender

  {
    [delegate dismissModalView:self];

 }

 -(void) dismissModalView:(UIViewController *) viewController;
{

 [self.dismissModalViewController Animated:YES];

}

@end

But somehow it is not working when clicking on done button

 UIButton* backButton = [UIButton buttonWithType:101]; 
  [backButton addTarget:self action:@selector(dismissView:)   forControlEvents:UIControlEventTouchUpInside];
  [backButton setTitle:@"Done" forState:UIControlStateNormal];

// create button item
UIBarButtonItem* backItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];

// add the button to navigation bar
self.navigationItem.leftBarButtonItem = backItem;
[backItem release];

Anyone have any clue that what i m missing in my code or what i m doing wrong. Help will be really appreciated.

Thanks

Upvotes: 0

Views: 97

Answers (3)

Hubert Kunnemeyer
Hubert Kunnemeyer

Reputation: 2261

You are pushing onto the navigation's stack so no Modal was ever displayed you need to pop the view off the stack:

[self.navigationController popViewControllerAnimated:YES];

instead of:

[self dismissModalViewControllerAnimated:YES];

Upvotes: 1

BufferStack
BufferStack

Reputation: 549

I would have liked to comment, but not enough privilege. Anyways AFAIK...

One does not 'push' a modal VC... it needs to be 'presented' like this..

[navigationController presentModalViewController:_viewController animated:YES];

Only a presented Modal VC will get dismissed on calling [self dismissModalViewControllerAnimated:YES];

OR, in case you really need to 'push' it.. then you need to 'pop' it to get back!

Hope it helps

Upvotes: 1

Sam Baumgarten
Sam Baumgarten

Reputation: 2259

Correct me if I'm wrong but wouldn't you want to define _viewController in your .h file and dismiss it with: [delegate dismissModalView:_viewController]; rather then dismissing self because self is not a view controller.

Upvotes: 1

Related Questions