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;
Related
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.
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.
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];
}
I have a view that pops up a UIAlertView if the name field for an item entry isn't populated when you attempt to save. I wanted to display my own custom alert view instead. I load it with a xib:
- (void)enterNameAlert {
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:#"AddNameAlertView" owner:self options:nil];
enterNameAlertView = [subviewArray objectAtIndex:0];
enterNameAlertView.frame = CGRectMake(232, 417, 303, 171);
enterNameAlertView.alpha = 0.0;
[self.view addSubview:enterNameAlertView];
[self.view bringSubviewToFront:enterNameAlertView];
//fade view in
[UIView animateWithDuration:.50f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void) {
enterNameAlertView.alpha = 1.0;
} completion:NULL];
[UIView commitAnimations];
}
However, you can still click on all the other elements on the view while the alert is displayed. Ideally, I'd like to make the alertview modal. As a hack, I just set xxxx.enabled = NO to all the other elements while the alertview is up, and switch them back to xxxx.enabled = YES when you press OK on the alert.
There has to be a better way to cancel all touches to self.view but not to self.enterNameAlertView.
If I do self.view.userInteractionEnabled = NO it cancels all touches to self.view as well as self.enterNameAlertView.
Any ideas?
I think the solution for your problem is that you have to introduce one more view in between your custom alert view and orignal view, once the alert comes you need to bring that view on your self.view and hide (or remove) it when your alert is dismissed, i have already done this and it work like charm for me. Hope this helps you too :)
Got it. I used a UIScrollView with interaction enabled. That stops touches from outside the custom alertview from being passed to the main view underneath.
- (void)enterNameAlert {
//pop a UIScrollView behind the alert so that the user can't touch the controls behind it
shadowScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 768, 1024)];
shadowScrollView.userInteractionEnabled = YES;
shadowScrollView.backgroundColor = [UIColor blackColor];
shadowScrollView.alpha = 0.0;
[self.view addSubview:shadowScrollView];
[self.view bringSubviewToFront:shadowScrollView];
//put the alertview in front of the disabled view to make it act like a modal view
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:#"AddNameAlertView" owner:self options:nil];
enterNameAlertView = [subviewArray objectAtIndex:0];
enterNameAlertView.frame = CGRectMake(232, 417, 303, 171);
enterNameAlertView.alpha = 0.0;
[self.view addSubview:enterNameAlertView];
[self.view bringSubviewToFront:enterNameAlertView];
//animate view in
[UIView animateWithDuration:.50f delay:0.0f options:UIViewAnimationOptionCurveEaseOut animations:^(void) {
shadowScrollView.alpha = .6;
enterNameAlertView.alpha = 1.0;
} completion:NULL];
[UIView commitAnimations];
}
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.