How do I switch views after signing up and logging in? - objective-c

So the backend is working, I can successfully create accounts and login. Now I want them to transition to the home screen so how I do I set so if the sigup/login is successful it transitions? This is my code:
- (IBAction)signUp:(UIButton *)sender {
NSString *name = _textUsername.text;
NSString *password = _textPassword.text;
NSString *email = [_textEmail.text lowercaseString];
//---------------------------------------------------------------------------------------------------------------------------------------------
if ([name length] == 0) { [ProgressHUD showError:#"Name must be set."]; return; }
if ([password length] == 0) { [ProgressHUD showError:#"Password must be set."]; return; }
if ([email length] == 0) { [ProgressHUD showError:#"Email must be set."]; return; }
//---------------------------------------------------------------------------------------------------------------------------------------------
[ProgressHUD show:#"Please wait..." Interaction:NO];
PFUser *user = [PFUser user];
user.username = name;
user.password = password;
user.email = email;
user[PF_USER_EMAILCOPY] = email;
user[PF_USER_FULLNAME] = name;
user[PF_USER_FULLNAME_LOWER] = [name lowercaseString];
[user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error)
{
if (error == nil)
{
ParsePushUserAssign();
[ProgressHUD showSuccess:#"Succeed."];
[self dismissViewControllerAnimated:YES completion:nil];
}
else [ProgressHUD showError:error.userInfo[#"error"]];
}];
Also any idea why my text field doesn't automatically go to the next one? Pretty sure the code is right. Thanks.
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
if (textField == _textUsername)
{
[textField resignFirstResponder];
[_textEmail becomeFirstResponder];
}
else if (textField == _textEmail)
{
[textField resignFirstResponder];
[_textPassword becomeFirstResponder];
}
else if (textField == _textPassword)
{
[textField becomeFirstResponder];
[_textBirthday becomeFirstResponder];
}
else if (textField == _textBirthday)
{
[self actionRegister];
}
return YES;

The way I arrange my parse.com apps is to have the main VC, the one that does the app's usual business for a logged-in user, be the storyboard's starting VC. It's first job in viewDidAppear, is to check that the PFUser currentUser exists (and is logged-in if your app requires it). If it isn't, then and only then, present the login/signup VC.
When login/signup dismisses as you have it in the posted code, viewWillAppear in your main VC fires again and detects the ready-to-run condition. It's action in that case is to carry on with initialization for a logged in user.
EDIT - For example, I have an app where the main VC is the starting VC in storyboard. It has a segue with identifier == #"GetUser" drawn from it to my equivalent of your signup/login VC.
Here's the (slightly simplified) viewDidAppear from my main VC.
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// I require only a non-nil currentUser to run the app. Your check
// might be more elaborate...
PFUser *user = [PFUser currentUser];
if (user) {
// put up a "busy" UI. In my app, it's just a view with UIActivityIndicator
// update the user so we know its current
[user fetchInBackgroundWithBlock:^(PFObject *object, NSError *error) {
// remove the "busy" UI
[self startTheApp];
}];
} else {
[self performSegueWithIdentifier:#"GetUser" sender:self];
}
}
-(void)startTheApp {
// fetch data from parse.com and reload the table view that's the
// centerpiece of the UI for this VC.
}
The nice behavior here is when the login/signup succeeds, the currentUser is non-nil, so when the login/signup VC is dismissed, viewDidAppear as shown above is fired again because the main VC underneath is being revealed again. And on that second time through, the currentUser passes the starting condition and we go to startTheApp.

Related

How to add 'cancel' button to camera acquisition code?

I have this code which captures a barcode; it works fine, except on an iPad, if you can't/don't hold the iPad steady, it "tries until the cows come home" if you get my drift. I want to add a "cancel" button or figure out some way to cancel the method so I don't have to kill the app and start it again. Here is my code:
- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection {
CGRect highlightViewRect = CGRectZero;
AVMetadataMachineReadableCodeObject *barCodeObject;
NSString *detectionString = nil;
NSArray *barCodeTypes = #[AVMetadataObjectTypeEAN13Code];
for (AVMetadataObject *metadata in metadataObjects) {
for (NSString *type in barCodeTypes) {
if ([metadata.type isEqualToString:type])
{
barCodeObject = (AVMetadataMachineReadableCodeObject *)[_prevLayer transformedMetadataObjectForMetadataObject:(AVMetadataMachineReadableCodeObject *)metadata];
highlightViewRect = barCodeObject.bounds;
detectionString = [(AVMetadataMachineReadableCodeObject *)metadata stringValue];
break;
}
}
if (detectionString != nil) {
_label.text = detectionString;
oISBNField.text = detectionString; // move detectionString to ISBN textbox
[_session stopRunning];
[_prevLayer removeFromSuperlayer];
[_label removeFromSuperview];
break;
}
else
_label.text = #"(none)";
}
}
Can someone please give me some help on this? I would really, really appreciate it! :D
Just create a cancel button and add it to the UIViewController's view.
When the button is pressed, stop the capture session and dismiss the presented view controller.
- (void)cancelButtonPressed:(id)sender {
[self.captureSession stopRunning]; //stop the capture session
[self.presentingViewController dismissViewControllerAnimated:YES completion:nil]; // dismiss the current view controller
}

Objective C Objects Only Being Changed In Method Scope

I am making a chat application, and when the person logs onto the chat successfully, the server replies with the people that are online at that moment. That string (the server sends a string) gets converted to a NSMutableArray, which is then stored into a NSMutableArray called tableData, which is the data source for a NSTableView. When the online people are stored into tableData, the NSLog output shows that tableData is filled. However, when that method is done, and the login view is closed, in the debugger tableData says 0 Objects, and the NSTableView doesn't fill, which it normally does. Here is are my methods (one calls another):
- (void)getPeople:(NSString *)outputMessage
{
NSArray *newTableData = [outputMessage componentsSeparatedByString:#";"];
self.tableData = [NSMutableArray arrayWithArray:newTableData];
//[self.tableData removeObjectAtIndex:0];;
NSLog(#"Data: %#", self.tableData);
[self.people reloadData];
}
- (void)accessGrantedWithOnlineUsers:(NSString *)users
{
NSLog(#"Access Granted");
self.isLoggedIn = YES;
[self.loginButton setTitle:#"Logout"];
[self getPeople:users];
}
Here is my method that opens and closes the login view:
- (IBAction)loginToChat:(id)sender {
NSLog(#"Called");
if (self.loginPopover == nil) {
NSLog(#"Login Popover is nil");
self.loginPopover = [[NSPopover alloc] init];
self.loginPopover.contentViewController = [[LoginViewController alloc] initWithNibName:#"LoginViewController" bundle:nil];
}
if (!self.loginPopover.isShown) {
NSLog(#"Login Popover is opening");
[self.loginButton setTitle:#"Cancel"];
[self.settingsButton setEnabled:NO];
[self.send setEnabled:NO];
[self.message setEnabled:NO];
[self.loginPopover showRelativeToRect:self.loginButton.frame ofView:self.view preferredEdge:NSMinYEdge];
}
else {
NSLog(#"Login Popover is closing");
if (self.isLoggedIn) {
[self.loginButton setTitle:#"Logout"];
}
else {
[self.loginButton setTitle:#"Login"];
}
[self.settingsButton setEnabled:YES];
[self.send setEnabled:YES];
[self.message setEnabled:YES];
[self.loginPopover close];
}
}
Any help would be greatly appreciated because I have a deadline for this project.
Instead of
self.tableData = [NSMutableArray arrayWithArray:newTableData];
can you try
self.tableData = [newTableData copy];
The only thing i can think of is - is the tableData a strong property?
After this
self.tableData = [NSMutableArray arrayWithArray:newTableData];
write this snippet:
[self setTableData:tableData]

Hide Copy and Deselect UITextView options after copying all text

I am working on a messaging app. I want to give a "copy" option to the user when they enter their message in a UITextView. When the user presses the "copy" button, it is copying the message, but the popover shows again and again, and the text is still selectable.
I don't know how to control this. I have pasted some source code for your reference.
I wrote a sub class for UITextView.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
NSLog(#"Action : %#", NSStringFromSelector(action));
NSLog(#"Sender : %#", sender);
if (action == #selector(copy:))
{
[self selectAll:self];
//return [super canPerformAction:action withSender:sender];
return YES;
}
else if (action == #selector(cut:))
{
return NO;
}
return NO;
}
I have solved my problem. I have used below codes to solve.
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == #selector(copy:))
{
[self selectAll:self];
return YES;
}
else if (action == #selector(cut:))
{
return NO;
}
return NO;
}
- (void)copy:(id)sender
{
UIPasteboard *pastBoard = [UIPasteboard generalPasteboard];
[pastBoard setString:self.text];
self.selectedTextRange = nil;
[self resignFirstResponder];
}
Thanks to Mr.Vimal Venugopalan and Mr.Mrueg. It is working for me. It will help to some one.
If you are using the iOS5
UITextView adopts the UITextInput protocol, which has a selectedTextRange property. Set the property to nil:
Add the below code just above the last return NO.
self.selectedTextRange = nil;
Hope this helps

UIImagePickerController issue?

This is my code to present the UIImagePickerController:
// Show the media browser with our settings, then the browser will call our delegate if needed
- (BOOL) startMediaBrowserFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *mediaUI = [[[UIImagePickerController alloc] init] autorelease];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
// Check for images type
NSArray * availableTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum];
BOOL imgsOnlyAvailable = NO;
// Check if we have only images
for (int i = 0; i < [availableTypes count]; i++) {
// Convert the type
CFStringRef type = (CFStringRef) [availableTypes objectAtIndex:i];
if (CFStringCompare ((CFStringRef) type, kUTTypeImage, 0) == kCFCompareEqualTo) {
// We have images
imgsOnlyAvailable = YES;
break;
}
}
// Check if they are available
if (imgsOnlyAvailable == NO)
return NO;
// Displays only saved pictures from the Camera Roll album.
mediaUI.mediaTypes = [NSArray arrayWithObject:(id) kUTTypeImage];
// Hides the controls for moving & scaling pictures, or for
// trimming movies. To instead show the controls, use YES.
mediaUI.allowsEditing = NO;
mediaUI.delegate = delegate;
[controller presentModalViewController: mediaUI animated: YES];
return YES;
}
// Picker delegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage;
// Handle a still image picked from a photo album
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
== kCFCompareEqualTo) {
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
// Set image
self.imgPic.image = originalImage;
// Now set the button to enabled
self.btnToText.enabled = YES;
}
// Hide picker selector
[[picker parentViewController] dismissModalViewControllerAnimated: YES];
[picker release];
}
The problem is whenever I try to click an image in the UIImagePickerController nothing happens (usually it chooses the image then will dismiss the Controller)
Is there any reason why things are going wrong like this?
Thanks!
// Select the image
- (IBAction)btnSelectFileTouchUp:(id)sender {
// Start the media browser
if ([self startMediaBrowserFromViewController:self usingDelegate:self] == NO) {
// Can't open the media browser
UIAlertView * alrt = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Can't open the photo media browser in this device" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[alrt show];
[alrt release];
}
}
Check out the documentation for parentViewController in the UIViewController Class Reference. Specifically this little tidbit.
Prior to iOS 5.0, if a view did not have a parent view controller and
was being presented modally, the view controller that was presenting
it would be returned. This is no longer the case. You can get the
presenting view controller using the presentingViewController
property.
In this instance, I expect that parentViewController is nil on iOS 5 and that is why the controller will not dismiss. Try replacing parentViewController with presentingViewController.
Update: You'll have to check for the existence of presentingViewController on UIViewController to provide behavior for iOS versions < 5.0.
UIViewController *dismissingController = nil;
if ([self respondsToSelector:#selector(presentingViewController)])
dismissingController = self.presentingViewController;
else
dismissingController = self.parentViewController;
[dismissingController dismissModalViewControllerAnimated:YES];

ShareKit modal view controller won't go away

I'm using ShareKit 0.2.1 on Xcode 4.2 (iOS SDK 5) to share text on Twitter. It shares fine, but the modal view controller wont go away after successfully sharing on after clicking on the cancel button (see below):
And this is my code:
-(IBAction)shareOnTwitter:(id)sender{
// Item to share
NSString *text = #"Go away, modal view controller!";
[SHKTwitter shareText:text];
}
What am I doing wrong?
It is an iOS 5 issue. It's because ShareKit is using a method on UIViewController called parentViewController and according to the Apple docs you can no longer use this in iOS 5. Instead, you must use presentingViewController.
So to fix it in the ShareKit code, go into SHK.m, find the method with signature (void)hideCurrentViewControllerAnimated:(BOOL)animated, and replace it with:
- (void)hideCurrentViewControllerAnimated:(BOOL)animated
{
if (isDismissingView)
return;
if (currentView != nil)
{
// Dismiss the modal view
if ([currentView parentViewController] != nil)
{
self.isDismissingView = YES;
[[currentView parentViewController] dismissModalViewControllerAnimated:animated];
} else if ([currentView presentingViewController] != nil) {
self.isDismissingView = YES;
[[currentView presentingViewController] dismissModalViewControllerAnimated:animated];
} else
self.currentView = nil;
}
}
This works for me on iOS 5.
if (isDismissingView)
return;
if (currentView != nil)
{
// Dismiss the modal view
if ([currentView parentViewController] != nil)
{
self.isDismissingView = YES;
[[currentView parentViewController] dismissModalViewControllerAnimated:animated];
}
else {
//## ADD BELOW ##
self.isDismissingView = YES;
[currentView dismissModalViewControllerAnimated:animated];
self.currentView = nil;
}
}
else {
[[self getTopViewController].navigationController popViewControllerAnimated:YES];
}
This is the code I use in one of my apps.
It dismisses fine.
NSURL *url = [NSURL URLWithString:#"http://itunes.apple.com/us/app/packager/id459511278?l=nl&ls=1&mt=8"];
NSString *twittertext = [[NSString alloc] initWithFormat: #"Tweet Text"];
SHKItem *item = [SHKItem URL:url twittertext];
// Share the item
[SHKTwitter shareItem:item];
[twittertext release];
I have used the following code in my app (ARC disabled)
NSString *text = #"Go away, modal view controller!";
[SHKTwitter shareText:text];
I can confirm it dismisses fine.
You probably changed some code in SHKTwitterForm.m when attempting to make Sharekit ARC compatible. Which resulted in your bug