switch view iphone - switching to empty view - iphone

i got a 3 view controller - Register, Login, Profile
and i got a UISegmentedControl that i can switch views by from Register to main and from Login to main too.
but when i'm in Register view for Example id like to switch users to profile view after finishing registration.
and here's the Code..
-(IBAction) switchView :(id)sender{
switch (segControl.selectedSegmentIndex) {
case 0:
{
HangmanViewController * main =[[HangmanViewController alloc]initWithNibName:nil bundle:nil];
main.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:main animated:YES];
[main release];
}
break;
case 1:
break;
case 2:
{
Profile *profile2 = [[Profile alloc]initWithNibName:nil bundle:nil];
[self presentModalViewController:profile2 animated:YES];
[profile2 release];
}break;
default:
break;
}
}
and i Already imported the .h file of Profile Class..
so what's wrong !!!!
Thanks.

i don't understand you question perfectly hope this will help you.
- (IBAction)SegmentControll:(id)sender {
if (SegmentControll.selectedSegmentIndex==0) {
//your main view
}
if (SegmentControll.selectedSegmentIndex==1) {
//your Profile view
}
}

Do you want to switch users to profile view only after Successfully finishing registration/login otherwise show alert ?
then you may try this :
in your .h file
NSString *count;
in your .m file
#Synthesize count;
in your ViewdidLoad
count=#"1";
//so here default value of count is 1
// after Successfully finishing registration/login make count =2
count=#"2";
In your SegmentControl Action make this condition :
- (IBAction)SegmentControll:(id)sender
{
if (SegmentControll.selectedSegmentIndex==0)
{
if (count isEqualToString:#"2") {
//your main view
}
else
// alert for login/Register
}
if (SegmentControll.selectedSegmentIndex==1) {
//your Profile view
}
if (SegmentControll.selectedSegmentIndex==2) {
//your Register view
}
}

Related

MBProgressHUD activity indicator doesn't show in viewDidAppear after segue is performed

I have two UITableViewControllers A and B, and this is what I'm trying to do when I click on a table view cell in A:
Prepare to segue from A to B by setting some of B's variables from A.
Perform segue from A to B.
B appears.
Display a "Loading" activity indicator with [MBProgressHUD][1].
In a background task, retrieve data from a URL.
If an error occurs in the URL request (either no data received or non-200 status code), (a) hide activity indicator, then (b) display UIAlertView with an error message
Else, (a) Reload B's tableView with the retrieved data, then (b) Hide activity indicator
However, this is what's happening, and I don't know how to fix it:
After clicking a cell in A, B slides in from the right with an empty plain UITableView. The MBProgressHUD DOES NOT SHOW.
After a while, the tableView reloads with the retrieved data, with the MBProgressHUD appearing very briefly.
The MBProgressHUD immediately disappears.
There doesn't seem to be an error with the way the background task is performed. My problem is, how do I display the MBProgressHUD activity indicator as soon as my B view controller appears? (And actually, how come it's not showing?) Code is below.
A's prepareForSegue
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
B *b = (B *)[segue destinationViewController];
// Set some of B's variables here...
}
Relevant methods in B
- (void)viewDidAppear:(BOOL)animated {
[self startOver];
}
- (void)startOver {
[self displayLoadingAndDisableTableViewInteractions];
[self retrieveListings];
[self.tableView reloadData];
[self hideLoadingAndEnableTableViewInteractions];
}
- (void)displayLoadingAndDisableTableViewInteractions {
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = #"Loading";
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
self.tableView.userInteractionEnabled = NO;
}
- (void)hideLoadingAndEnableTableViewInteractions {
[MBProgressHUD hideHUDForView:self.view animated:YES];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
self.tableView.userInteractionEnabled = YES;
}
- (void)retrieveListings {
__block NSArray *newSearchResults;
// Perform synchronous URL request in another thread.
dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
newSearchResults = [self fetchNewSearchResults];
});
// If nil was returned, there must have been some error--display a UIAlertView.
if (newSearchResults == nil) {
[[[UIAlertView alloc] initWithTitle:#"Oops!" message:#"An unknown error occurred. Try again later?" delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil] show];
} else {
// Add the retrieved data to this UITableView's model. Then,
[self.tableView reloadData];
}
}
- (NSArray *)fetchNewSearchResults {
// Assemble NSMutableArray called newSearchResults from NSURLConnection data.
// Return nil if an error or a non-200 response code occurred.
return newSearchResults;
}
I think you have to call [self hideLoadingAndEnableTableViewInteractions]; after newSearchResults = [self fetchNewSearchResults]; You are retrieving data in another thread which means -startOver will continue executing after calling [self retrieveListings]; and will hide the HUD right away. Also because you are updating the display you have to make sure you are doing that on the main thread. See example
dispatch_async(dispatch_get_main_queue(), ^{
//update UI here
});
When B appears, it displays a plain and empty UITableView, but does not display the MBProgressHUD even if the task does begin in the background (and yet, the MBProgressHUD is called to show before that). Hence, my solution is to show the MBProgressHUD in viewDidLoad, which precedes viewWillAppear.
- (void)viewDidLoad {
// ...
[self displayLoadingAndDisableUI];
}
I set up two additional boolean properties to B--one in .h, called shouldStartOverUponAppearing, and one in a class extension in .m, called isLoadingAndDisabledUI. In startOver, I added the following lines:
- (void)startOver {
if (!self.isLoadingAndDisabledUI) {
[self displayLoadingAndDisabledUI];
}
}
The check is done so that startOver doesn't display another MBProgressHUD when it has already been displayed from viewDidLoad. That is because I have a third view controller, called C, that may call on B's startOver, but doesn't need to call viewDidLoad just to display the MBProgressHUD.
Also, this is how I defined viewDidAppear:
- (void)viewDidAppear:(BOOL)animated {
if (self.shouldStartOverUponAppearing) {
[self startOver];
self.shouldStartOverUponAppearing = NO;
}
}
This way, startOver will only be invoked IF B appeared from A. If B appears by pressing "Back" in C, it will do nothing and only display the old data that was there.
I think that this solution is FAR from elegant, but it works. I guess I'll just ask for a better approach in a separate SO question.
I have used a common method for MBProgressHUD.
#import "MBProgressHUD.h" in AppDelegate.h also following methods.
- (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title;
- (void)dismissGlobalHUD;
In AppDelegate.m add following methods.
- (MBProgressHUD *)showGlobalProgressHUDWithTitle:(NSString *)title {
[MBProgressHUD hideAllHUDsForView:self.window animated:YES];
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.window animated:YES];
hud.labelText = title;
return hud;
}
- (void)dismissGlobalHUD {
[MBProgressHUD hideHUDForView:self.window animated:YES];
}
How to use?
AppDelegate *appDel = (AppDelegate *)[[UIApplication sharedApplication]delegate];
//Show Indicator
[appDel showGlobalProgressHUDWithTitle:#"Loading..."];
//Hide Indicator
[appDel dismissGlobalHUD];
Hope this helps.

Wait UIAlertView Button

I have this code in my Application:
if (self.uiSwtichState == TRUE)
{
if ([CLLocationManager locationServicesEnabled])
{
[self.textString stringByAppendingString:[[[SMSLocationModel alloc] init] getLocation]];
}
}
MFMessageComposeViewController *sms = [[MFMessageComposeViewController alloc] init];
sms.messageComposeDelegate = self;
sms.body = self.textString;
sms.recipients = self.arrayOfNumbers;
[self presentModalViewController:sms animated:YES];
this code is into a method called "send".
As you can imagine, the method getLocation get the current user location and, the first time the Application run, a UIAlertView appear on the screen asking the user if he want to allow the application to get his location.
The problem is that when this UIAlertView appear, she is immediately replaced by the sms Vie Controller (MFMessageComposeViewController) and in this way the user doesn't have enough time for answer YES or NO in the UIAlertView.
There is a way for wait that the user press a button in the UIAlertView and THEN present the ModalViewController?
Thanks!
you could use the CLLocationManagers delegate method to wait until the authorization state changes. Save the sms viewController to an instance variable if you are not authorized to use location services. then ask the location manager to authorize you. If the authorization state changes present that view controller.
// somewhere else
self.locationManager.delegate = self;
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
NSLog(#"New Authorization State: %d", status);
if (self.viewControllerToPresentAfterLocationDidChange) {
if (status == kCLAuthorizationStatusAuthorized) {
// set location of viewController
[self presentViewController:self.viewControllerToPresentAfterLocationDidChange animated:YES completion:NULL];
}
else {
// user did not authorize you to use location services
}
self.viewControllerToPresentAfterLocationDidChange = nil;
}
}
- (IBAction)buttonPressed:(id)sender {
UIViewController *vc = [[UIViewController alloc] init];
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
// authorized already, no alert will appear.
// set location of vc
[self presentViewController:vc animated:YES completion:NULL];
}
else if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
self.viewControllerToPresentAfterLocationDidChange = vc;
// access location so alert will show
}
else {
// not authorized or restricted.
}
}
Maybe you can use this delegate:
- (void)didPresentAlertView:(UIAlertView *)alertView{
}
For example, initializing a boolean which can prevent of presenting this modal view controller. Then if dismissed change the boolean to allow presenting this view controller.
Yes, you can implement the AlertViewDelegate.
See UIAlertViewDelegate Documentation for further information.
Then you can use the alertView:clickedButtonAtIndex: method and present your MessageComposer when the user has clicked the alert view.

How to pass value to a variable defined in parent class from child class?

My home page has a toolbar with three buttons home,notification and login. I want notification button show only when user loged In. here is my code
//Parent class
#interface Toolbar : UIViewController {
UIToolbar *toolBar;
UIButton *home;
UIButton *notification;
UIButton *login;
BOOL signIn;
}
#property(nonatomic,assign)BOOL signIn;
#property(nonatomic,retain)UIButton *home;
#property(nonatomic,retain)UIButton *notification;
#property(nonatomic,retain)UIButton *login;
#property(nonatomic,retain)UIToolbar *toolBar;
-(IBAction)Home:(id)sender;
-(IBAction)LogIn:(id)sender;
//Toolbar.m
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
if (!signIn) {
[notification setHidden:YES];
[login setHidden:NO];
}else {
[notification setHidden:NO];
[login setHidden:YES];
}
}
//Child class
#interface LoginScreen : Toolbar{
LoginScreen.m
-(IBAction)SignIn{
if ([emailField.text isEqualToString:#"m"] && [passwordField.text isEqualToString:#"a"]) {
NSLog(#"loggedIN");
signIn=YES;
Home *home=[[Home alloc]initWithNibName:#"Home" bundle:nil];
[self.navigationController pushViewController:home animated:YES];
[home release];
}else {
//other condition
}
}
problem is I am able to to go back to home but signIn condition is not working. please guide me . Thanks..
Use Protocols (delegate mechanism) to send back the value to the parent view controller.
See here for some sample. Also here
You can find many samples for this in every Apple sample application.
I have also give you a Sample Application. Go through that and modify the protocol as per your need

how to fix iOS leak when flipping between two views

My iPhone app badly leaks when flipping back and forth between a main uiviewcontroller and a help uiviewcontroller .
Here is the source of the main view, followed by source of the help view.
MAIN VIEW - FLIP TO HELP.....................
// Changes from operational view to Help view.
- (IBAction)showHelp:(id)sender
{
// End trial mode:
self.stop_trial_if_started;
self.rename_trial_if_edited;
// Switch to trial help:
help_view_context = 0;
HelpView *controller = [[HelpView alloc] initWithNibName:#"HelpView" bundle:nil];
controller.delegate = self;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:controller animated:YES];
[controller release];
}
HELP VIEW - INIT.............................
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
help_scroll.editable = FALSE;
return;
}
HELP - RETURN TO MAIN VIEW.........................
// User clicked the button to return to operational view:
- (IBAction)done:(id)sender {
NSLog(#"help- done");
if( help_view_context == 0 ) {
[self.delegate trial_help_DidFinish:self];
}else{
[self.delegate file_help_DidFinish:self];
}
}
MAIN VIEW - RETURN FROM HELP...............................
// Inits operational view when user changes from Help view back to operational view.
- (void)trial_help_DidFinish:(HelpView *)controller {
NSLog(#"trial_help_DidFinish");
[self dismissModalViewControllerAnimated:YES];
self.init_trial_operation;
}
You are creating a controller with ref count of 1 and a local reference each time showHelp: is called:
HelpView *controller = [[HelpView alloc] initWithNibName:#"HelpView" bundle:nil];
you are losing your reference to it at the end of this method.
You happen to have references to it in done: (self) and *_help_didFinish (controller), but you never release it in either of those locations. Dismissing the controller is fine, but you also have to release it.
(Another option would be to never create a second one, and maintain an iVar to the original.)
You could well be leaking on this line
controller.delegate = self;
What is your property declaration for the delegate. If it's anything other than assign, then you either need to change it (preferred option) or make sure you are releasing it in the dealloc method of HelpView controller.

Detecting when modal view has been displayed

This is a fun one... I have an application that has a help screen and is displayed as a modal view. The main view has an action that occurs when the device is shaken. I do not want the action to occur (sounds are played) when the help screen has been displayed.
I have tried a few things... here is my code:
To display the help screen:
- (IBAction)helpButtonPressed:(id) sender {
helpViewController = [[HelpViewController alloc] init];
[self presentModalViewController:helpViewController animated:YES];
}
To release the help screen:
- (IBAction)buttonPressed:(id) sender {
[self dismissModalViewControllerAnimated:YES];
}
I tried the following with no success:
if ([helpViewController.view isHidden ]) {
NSLog(#"Shake -- helpView is loaded");
} else {
NSLog(#"Shake -- helpView is not loaded");
}
if ([helpViewController isViewLoaded]) {
NSLog(#"Shake -- helpView is loaded");
} else {
NSLog(#"Shake -- helpView is not loaded");
}
if ([self isViewLoaded]) {
NSLog(#"Shake -- helpView is loaded");
} else {
NSLog(#"Shake -- helpView is not loaded");
}
What I was thinking is if there is a function to allow me to detect if the help view is showing, I will return without playing the sounds when the device is shaken....
Any ideas?
I'm assuming that the view controller which loads the modal controller is also the view controller that responds to the shake action. If that's the case, then you can use the parent view controller's modalViewController property to see whether the modal controller is active:
if(self.modalViewController != nil) {
// Modal view controller is active; do nothing
NSLog(#"Shake -- helpView is loaded");
return;
} else {
// No modal view controller; take action
NSLog(#"Shake -- helpView is not loaded");
[self performSomeAction];
}