Button to restart app - objective-c

I've been making an app that I need help with. After I do what I want the app to do, it goes to a new page. when I press the go back button, it is still in the same state as when I left. I want the first view to completely reset when I hit the button. Any suggestions as to how to do this? Any help is appreciated!
Here is code for what the button currently does:
- (IBAction)retry:(id)sender
{
[self dismissModalViewControllerAnimated:YES];
}

Implement viewWillAppear on your initially displayed viewController as following:
- (void)viewWillAppear:(BOOL)animated
{
//
//put your code to reset this viewController into its initial state here
//
[self resetAllStatesBackToDefault];
}
As an example, suppose you got a UITextField on that first viewController that is named textField;
- (void)resetAllStatesBackToDefault
{
self.textField.text = #"";
}

What you need is the -(void)viewDidAppear:(BOOL)animated;

Related

How to perform IBAction before dismiss sheetViewController from same UIButton?

I am writing a Cocoa Mac app in Objective-C and using Storyboard for my UI.
I have a "Confirm" button in my sheetViewController.m which I want to perform some action (save some settings) as well as dismiss the sheetViewController at the same time. They both use the sheetViewController.m as outlets.
Unfortunately, with Storyboard, I can only pick one received action (IBAction) or dismissController.
I want to perform the IBAction FIRST, before dismissing the sheet. How can I accomplish this?
Happy to do this in code as well instead of Storyboard if necessary!
Thanks!
You can use codes to dismiss controller in your IBAction.
I found out how to reference Storyboard ID in code:
On MainViewController:
- (IBAction)didClickButton:(NSButton *)sender {
SheetViewController *sheetViewController = [self.storyboard instantiateControllerWithIdentifier:#"SheetViewController"];
// Must match Storyboard ID
[self presentViewControllerAsSheet:sheetViewController];
}
On SheetViewController:
- (IBAction)didClickButton:(NSButton *)button {
// Do something
[self dismissViewController:self];
}

UISwitch feels sticky. How do I toggle the UISwitch on press?

Ive got this code and everything works correctly. But when I'm testing my app the switch is really hard to turn over on the UI.
-(IBAction)mySwitch:(UISwitch *)sender {
if([_theSwitch isOn]){
[_theSwitch setOn:NO animated:YES];
}
else if (![_theSwitch isOn]) {
[_theSwitch setOn:YES animated:YES];
}
}
is there any way that I can make it like the iOS 7 settings switches where it toggles on and off by button press rather than slide?
The reason is the code you've added. There is no reason to tell the switch which state it is in. It knows that info already. Remove all of that code and it should solve the issue.

iOS7:editable textfield cells in tableview is not working properly

Editable textfield cells Tableview is causing problem on keyboard tab button everytime it is calling textfieldshouldbeginediting even if i am in first textfield it is not going to nextfield.
It is going to last textfield and if popover is availabe it will crash.How can i fix this so that if enter tab then it has to resign current responder in textfielddidendediting and it should not go to textfieldshouldbegin editing.
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
PickerViewController *selectOperatorController;
NSLog(#"tag %d",textField.tag);
return NO;
}
I also declared textfield delegates like didendediting and shouldendediting
-(BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
activeField = nil;
if (self.chooseOperatorController) {
[self.chooseOperatorController dismissPopoverAnimated:YES];
}
return YES;
}
This is not as issue in iOS 6.But it is in iOS 7.
textfieldshouldbegin editing will not allow desktop keyboard tab button input,it cannot judge to move between the textfields.If we add textfielddidbeginediting we can move the controls,eventhough we have two methods we can move by using keyboard tab.So textfielddidbeginediting is mandatory if we want to move bewtween available textfields.

subview from UIWindow and sending it back to UIwindow

I have an UIWebView loaded with div, act as editor to write. Now i am adding UIWebView as sub view on UIWindow to set the frame equal to full screen and to hide the UIKeyboard, but at some button method, i need to get UIWebview from UIWindow and sent it back to UIKeyboard. Here is my code which is not working:
keyboardWindowFrame= nil;
for (UIWindow *testWindow in [[UIApplication sharedApplication] windows])
{
if (![[testWindow class] isEqual:[UIWindow class]])
{
keyboardWindowFrame = testWindow;
[webViewForEditing setFrame:CGRectMake(5, 63, 310, 400)];
[webViewForEditing.scrollView setContentSize:CGSizeMake(310, 400)];
[keyboardWindowFrame addSubview:webViewForEditing];
break;
}
}
- (IBAction)keyboardButtonSelected:(id)sender
{
[keyboardWindowFrame sendSubviewToBack:webViewForEditing]; //need to send UIWebView back to UIWindow so i can write
}
I think I am trying to do the same thing as you and so although your question is not particularly clear hopefully my answer will help.
It seems that you have a view 'webViewForEditing' which you want to add and bring in front of the keyboard. When you click a button you want to put this view behind the keyboard again.
I have also tried using the sendSubViewToBack code with no joy.
In the end though I managed to get it to work using:
[[self view] exchangeSubviewAtIndex: withSubviewAtIndex: ];
(Credit goes to Sunny with it adapted from a question here)
I used the following code below, with to switch between a UIView and the keyboard:
- (IBAction)toggleButtonPressed:(id)sender {
UIWindow * window = [UIApplication sharedApplication].windows.lastObject;
if (window.subviews.count == 1) {
[window addSubview:_menuView];
}
else {
[window exchangeSubviewAtIndex: 0 withSubviewAtIndex: 1];
}
}
I am only working with two views (_menuView and the keyboard) which means I check how many subviews my window has to make sure I only add _menuView once.
Then it is easy to exchange the two views.
Even if you had more subviews in your window I am sure you could use a modified version of this. As long as none of your other views change places then exchanging them will always switch the same two views.
NOTE: As an aside. I am not sure if this code works if called when the keyboard is not first responder. I get the variable for Window using the last object in the window, which is always the keyboard if it has been made first responder. It might need tweaking to get it working at other times.
I formulated another, more eloquent, answer while working on this:
Add the views you want to the window view that holds the keyboard view:
#interface BViewController : UIViewController {
UIWindow * window;
}
// Add the code when the keyboard is showing to ensure it is added while we have a keyboard and to make sure it is only added once
-(void) keyboardDidShow: (NSNotification *) notification {
window = [UIApplication sharedApplication].windows.lastObject;
if (window.subviews.count == 1) {
[window addSubview:_menuView];
[window addSubview:_gameView];
[window addSubview:_stickerView];
[self hideViews];
}
}
- (void)hideViews {
_menuView.hidden = YES;
_gameView.hidden = YES;
_stickerView.hidden = YES;
}
So now we have our UIWindow view which contains our views and also our keyboard. They are all hidden so our keyboard appears at the front and can be typed on.
Now use a simple function to decide which view to bring in front of the keyboard:
- (void)bringViewToFront: (UIView *)view {
[self hideViews];
view.hidden = NO;
}
Hiding the views makes sure we only have our correct view at the front.
I spent a lot of time thinking how we could move different views forward and back, using the exchange function. Actually using hide and reveal means we can get our view immediately and still easily have access to the keyboard by hiding all the views.

Give focus to an control when the App starts in Cocoa

I have multiple TextFiled controls, and can I put a focus on a specific TextField when the App starts?
EDIT
- (void)awakeFromNib
{
...
[commandInput becomeFirstResponder];
...
}
works.
yes, use
[textfield1 becomeFirstResponder]