How to figure out use a modal segue instead of push segue - ios6

I have this:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"segueFotosAloj"]) {
localGallery = [[FGalleryViewController alloc] initWithPhotoSource:self];
[self.navigationController pushViewController:localGallery animated:YES];
}
I want to use modal segues, so I can't have a PushViewController statement if I remove that line the Gallery don't load the images, how can I do it use a modal segue? (I don't want use push segue because I'm getting some errors about the navigationcontroller stack)

Here is an example code for modal segue. Just change the name of the view controller to the name of the view controller you are using in your project.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:#"segueFotosAloj"])
{
SomeViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"SomeIdentifier"];
[self.navigationController presentViewController:controller animated:YES completion:nil];
}
}
P.s, someViewController is the name you have to change.

Related

How to dismiss a popover in iPhone 5 iOS 6?

When my iPhone 5 app starts up I present no view controller because of the following:
- (void) viewDidAppear:(BOOL)animated {
}
The first thing the user will do is touch a button that is on the initial screen
that opens up a menu for retrieving a picture either from the user's
photo library, camera, or a picture of obama. The method that is called when that button is touched by the user is:
- (IBAction) pickImage {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:#"Image Source"
delegate:self cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Photo Library", #"Camera", #"Obama", nil];
[actionSheet showInView:scrollView];
[actionSheet release];
}
Now the actionSheet eventually calls:
[self presentViewController:imagePicker animated:TRUE completion:nil];
For the iPhone 5 everything works and the user is given 3 options with the presentViewController. After the user picks an image then I call:
[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
and the View Controller goes away. Now what I WOULD LIKE TO DO IS make the view controller appear initially so the user doesn't have to touch the button connected to pickImage by adding a line to the viewDidAppear method like so:
- (void) viewDidAppear:(BOOL)animated {
[self pickImage]; // makes the image picker pop up when app intializes
}
HOWEVER, when I test this, the imagePicker automatically appears like I expected but then after I finish picking my image and dismiss it, it reappears after I have chosen my image. HOW CAN I FIX THIS???
The following is the class I am talking about:
MainViewController.h FILE: http://db.tt/Vgm3w0gs
MainViewController.m FILE: http://db.tt/uN8YdNGN
Or you can just look at the following relevant methods:
- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex {
switch (buttonIndex) {
case 0: { //photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePicker animated:TRUE completion:nil];
} else {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Photo library is empty or unavailable" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alertView show];
}
break;
}
case 1: //camera option has it's view controller dismissed after image is taken. The rest of ActionSheet doesn't really matter...
HERE is the method that dismisses the ViewController except when imagePicker is called from within viewDidAppear. QUESTION: How can I get the viewController to dismiss when imagePicker is called from within viewDidAppear?
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// loads image into main screen
[self loadImage:[info objectForKey:UIImagePickerControllerOriginalImage]];
[[picker presentingViewController] dismissViewControllerAnimated:YES completion:nil];
} [picker release];
}
Your current problem seems to be that you want pickImage to be called when the view first appears, but not when it reappears as a result of a popup window closing.
One possibilty would be to move the pickImage call from the viewDidAppear callback into the viewDidLoad callback.
However, if that is being called too soon, the other option would be to add a boolean variable that you check in viewDidAppear to make sure that pickImage is only called once.
- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
if (firstAppearance) {
firstAppearance = NO;
[self pickImage];
}
}
And set that boolean to true in the viewDidLoad callback.
- (void) viewDidLoad {
[super viewDidLoad];
firstAppearance = YES;
}
Obviously you would also need to declare the bool in your header file somewhere.
BOOL firstAppearance;

self.tabBarController setSelectedIndex: not working

Things in the project:
a storyboard which contains just one Tab Bar Controller and four view controllers (so I have four tab bar items, and four segues, very very simple)
a custom Obj-Class which is used by the Tab Bar Controller, and draw a button on the "self.view"
What's the problem:
want to simulate "pressing the tab bar item" but not it is not working. help please. (I have not any other custom-obj-class but the only one for the UITabBarController)
What I've tried:
[(UITabBarController*)self.navigationController.topViewController setSelectedIndex:3];
[self.tabBarController setSelectedIndex:3];
[self.tabBarController setSelectedController
[self.tabBarController setSelectedViewController:[self.tabBarController.viewControllers objectAtIndex:3]];
custom-obj-class.m :
- (IBAction)buttonPressed:(UIButton *)sender
{
if (self.tabBarController) {
NSLog(#"I have a tab bar");
[self.tabBarController setSelectedIndex:1];
} else {
NSLog(#"I don't have"); // I GOT THIS ONE
}
return;
}
- (void)viewDidLoad
{
[super viewDidLoad];
...
UIBarButtonItem *realRoomItem = [[UIBarButtonItem alloc] initWithImage:realRoomImage style:UIBarButtonItemStylePlain target:self action:#selector(buttonPressed:)];
...
}
I'm sorry, there's a big mistake. It should be "self" but not "self.tabBarController" since my custom-obj-class is the class of UITabBarController and used in it also.

Button Action Issue in IOS

This is the code i have used.
In View Controller A:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(50, 50, 70, 40)];
[button setTitle:#"Next View" forState:UIControlStateNormal];
[button addTarget:self action:#selector(nextView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void) nextView
{
SecondviewController *secondView = [[SecondviewController alloc] init];
[self.view addSubview:secondView.view];
}
In View Controller B:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(50, 50, 70, 40)];
[button setTitle:#"Previous View" forState:UIControlStateNormal];
[button addTarget:self action:#selector(previousView) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void) previousView
{
[self.view removeFromSuperview];
}
Issue: When i click the button in the view controller B, its not switching back to the view controller A...
You are not switching viewControllers, you are taking the view from viewController B and adding it as a subview to viewController A.
Here:
SecondviewController *secondView = [[SecondviewController alloc] init];
[self.view addSubview:secondView.view];
You need to navigate to the new view Controller... Replace it with this for example
SecondviewController *secondViewController = [[SecondviewController alloc] init];
[self presentViewController:secondViewController animated:YES completion:NIL];
(it's best to include 'controller' when naming controllers to avoid confusion with their views)
Then to return, you need to dismiss the presented viewcontroller...
In ViewControllerB replace this:
[self.view removeFromSuperview];
With
[[self presentingViewController] dismissViewControllerAnimated:YES completion:NIL];
This is sending a message back from the presented viewController - viewController B - to the presenting viewController, viewControllerA which does the actual dismissing.
instead of adding a the second subview to the first subview, you need to present or push the view controller in the stack. You are just simply adding it as a subview.
SecondviewController *secondView = [[SecondviewController alloc] init];
[self presentViewController:secondView animated:NO completion:nil];
In the second view controller when you dismiss it you can just simply dismiss/pop it from the stack.
[self dismissViewControllerAnimated:YES];

Calling two different controllers from the same button based on a condition

I had the following code working before iOS6, but I cannot find an easy way to do it with iOS6, storyboards and Xcode 4.5
-(IBAction) ButtonPressed:(id)sender{
if(condition == 1]) {
FirstController *firstController = [[FirstController alloc]initWithNibName:nil bundle:nil];
firstController.delegate = self;
firstController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:firstController animated:NO completion:nil];
[firstController release];
}
SecondController *secondController = [[SecondController alloc]initWithNibName:nil bundle:nil];
secondController.delegate = self;
secondController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentViewController:secondController animated:NO completion:nil];
[secondController release];
}
This used to work fine. If I leave the code as is in iOS6, none of the controllers will show properly. Trying to transition the controller navigation to the storyboard does not help matters either since it appears I can have only one action to trigger a transition using a modal segue from ButtonPressed to either the one or the other.
Embedding the first controller in the viewdiload of the second controller has its own problems. If it is on the viewdidload will not work properly as other have experienced. If I put it on viewdidappear, the second controller will show up first creating an unpleasing UI. Any ideas?
A view controller can only present one view controller at a time. A different approach is required. For example, you can use a navigation view controller for what you wish to accomplish. If the user is logged in, you set the upload view controller as the root view controller of the navigation view controller. If the user is not logged in, you set the root view controller as the login view controller. When the user logs in, the login view controller will notify in its delegate that the user logged in, and you can then push the upload view controller into the navigation stack.
- (IBAction)buttonPressed:(id)sender{
UINavigationController* nvc;
if(condition == 1) {
FirstController *firstController = [[FirstController alloc]initWithNibName:nil bundle:nil];
firstController.delegate = self;
firstController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
nvc = [[UINavigationController alloc] initWithRootViewController:firstController];
[firstController release];
}
else {
SecondController *secondController = [[SecondController alloc]initWithNibName:nil bundle:nil];
secondController.delegate = self;
secondController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
nvc = [[UINavigationController alloc] initWithRootViewController:secondController];
[secondController release];
}
[self presentViewController:nvc animated:YES completion:nil];
[nvc release];
}
//Login view controller delegate method
- (void)loginViewControllerDidLoginSuccessfully:(FirstController*)loginViewController{
SecondController *secondController = [[SecondController alloc]initWithNibName:nil bundle:nil];
secondController.delegate = self;
secondController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[secondController.navigationItem setHidesBackButton:YES];
[loginViewController.navigationController pushViewController:secondController animated:YES];
[secondController release];
}

UITapGestureRecognizer won't trigger in IOS 6

The following code worked fine is IOS 5, but now handleTapGesture doesn't even get called in IOS 6. What changed?
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
[self addGestureRecognizer:tap];
}
return self;
}
- (void)handleTapGesture:(UITapGestureRecognizer *)recognizer
{
MessageNib *cell = self;
MessageView *view = (MessageView *)[[[NSBundle mainBundle] loadNibNamed:#"MessageView" owner:self options:nil] objectAtIndex:0];
view.message = cell.message;
[[NSNotificationCenter defaultCenter] postNotificationName:NEW_SUB_PAGE object:view];
}
I guess you have to set the numberOfTaps after alloc init
tap.numberOfTapsRequired = 1;
Or the other possibility is initWithCoder was not called.
Ended up just putting a "phantom" button (aka button with no content) over my item I wanted tappable and attached a Touch Up Inside event to the button which calls my tap gesture code.
This is a decent momentary hack anyway.

Resources