I have a alertview that starts in a view A and must stop in view B. How can I stop the alertview on B?
thks
How are you moving from A to B while a UIAlertView is displayed? Maybe post some code.
This scenario does not seem user-friendly, but there is a way you can dismiss the alert from A in B.
View A
Create an NSNotificationCenter and point it to a method that dismisses the alert:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(dismissAlert) name:#"dismissAlert" object:nil];
And the notification should call something like the following:
- (void) dismissAlert:(NSNotification *)notification
{
[alertView dismissWithClickedButtonIndex: 0 animated: YES];
}
View B
Now when you want to dismiss the alert, call the notification you created in View A:
[[NSNotificationCenter defaultCenter] postNotificationName:#"dismissAlert" object:nil];
Use dismissWithClickedButtonIndex:animated:
Related
I'am trying to call a function when the app com back to Foreground by applicationDidBecomeActiveNotification. I have it in iOS 8 and iOS 7 and it works well. But not in iOS 9 ? It docent call first time you open the app
-(void) viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(applicationDidBecomeActiveNotification:)
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
}
- (void)applicationDidBecomeActiveNotification:(NSNotification *)notification {
//stuff
}
It is every notification center that is not calling at startup...
I hade the same problem and for me it worked to move the addObserver code to awakeFromNib. Another solution could be to add a delay to the addObserver as in the example below:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(applicationDidBecomeActive) name:UIApplicationDidBecomeActiveNotification object:nil];
});
See Apple Foundation update for NotificationCenter
This is what you need,
NSNotificationCenter.defaultCenter().addObserverForName(UIApplicationDidBecomeActiveNotification, object: nil, queue: nil) { note in
self.applicationDidBecomeActiveNotification(note)
}
I had a main view name "View control" and two sub views view1 and view2... My application loads with view1 as a sub view in the main view .. I had a "Next" button on main view . when I press it..view1 should be replaced with view2 with in the main view... Any idea?
You have to use NSNotificationCenter.
Assuming you have 3 views, One main view and 2 subviews.
Main view = ContainerView
Subview1= HomeViewController
Subview2= nextViewcontroller
in Subview1 use following code.
[[NSNotificationCenter defaultCenter] postNotificationName:#"ChangetoView2" object:nil];
in Subview2 use following code.
[[NSNotificationCenter defaultCenter] postNotificationName:#"ChangetoView1" object:nil];
use the below code in the init method of ContainerView.
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ChangeTheView1:) name:#"ChangetoView1" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(ChangeTheView2:) name:#"ChangetoView2" object:nil];
And in the same class use the following function.
- (void)ChangeTheView1:(NSNotification *)notification
{
// Change here.
[bViewcontroller.view RemoveFromSuperview];
aViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
aViewController.showLogin = NO;
[self.containerView addSubview:aViewController.view];
companyLogoImage.hidden = YES;
}
- (void)ChangeTheView2:(NSNotification *)notification
{
[aViewcontroller.view RemoveFromSuperview];
bViewController = [[SomeViewController alloc] initWithNibName:#"SomeViewController" bundle:nil];
[self.containerView addSubview:bViewController.view];
}
I was wondering how to swipe the ViewController with a visible keyboard?
in iOS 7 I can swipe the ViewController from side to side, but the keyboard stays put.
in short, I would like to get to the following state:
Thanks!
Update:
I can't recommend the original solution. While it performed well (when it performed at all), it was an unreliable hack, and could easily break the pop gesture recognizer.
My colleague Dave Lyon came up with a great solution using iOS 7 view controller transitions and packaged it up into a pod:
https://github.com/cotap/TAPKeyboardPop
Once installed, just import the main file and you should be good to go.
Original:
I'd love to know if there's a better way of doing this, but I was able to achieve the behavior by adding the keyboard's view as a subview of the view controller's main view:
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.inputAccessoryView = [UIView new];
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:#selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillHide:(NSNotification *)note
{
if (self.textView.isFirstResponder) {
UIView *keyboardView = self.textView.inputAccessoryView.superview;
if (keyboardView) {
dispatch_async(dispatch_get_main_queue(), ^{
[self.view addSubview:keyboardView];
});
}
}
}
I've found you can also animate the keyboard with the gesture (via addTarget:action:), but the performance is abysmal and doesn't cleanly animate if the gesture is prematurely canceled.
when I get local notification I want to open my specific screen.
currently in my app i have used both navigation controller as well as model view controller so at the time of navigation controller, app is switching any view but when model controller exit . it is not opening the screen.
Plz suggest any solution?
There are two way to launch app when notification comes.
1- app is running in background.then open specific screen like
- (void)application:(UIApplication *)application didReceiveLocalNotification: (UILocalNotification *)notification
{
// write this line
[[NSNotificationCenter defaultCenter] postNotificationName:#"reloadData" object:self];
}
in which controller class you are create notification.
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadTable)
name:#"reloadData"
object:nil];
}
- (void)reloadTable
{
// create object of that controller which your want to open.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
AddToDoViewController *cvc = (AddToDoViewController *)[sb instantiateViewControllerWithIdentifier:#"AddToDo"];
[self presentModalViewController:cvc animated:YES];
}
I am using a Master-Detail template.
I have a segmented control in the detail view and I have set the MasterViewController as the delegate.
This enables me to give the user choices.
I know the segmented control is working and passing the choices to the MVC.
I want each choice to trigger a new set of data which can then be loaded into the table view in the MasterViewController.
My problem is that I cannot then find a way to update the data in the table view.
You can use an instance variable declared in MasterViewController to be updated while UISegmentedControl changes. Based on that, When you come back to the MasterViewController; use its viewWillAppear to [tableView reloadData]
Tell me if I am getting your question wrong.
EDIT:
Take an integer name it segmentIndex; update as and when Segment gets changed. Based on the value of segmentIndex load the dataSource needed to be displayed on UITableView of your MasterViewController
EDIT 2:
When UISegmentedControl's value changes, put mvc.segmentIndex = (currentValueOfYourSegmentedControl);
Then in MasterViewController's viewWillAppear
switch (self.segmentIndex)
{
case 0:
// Set Datasource for First Choice.
break;
case 0:
// Set Datasource for Second Choice.
break;
case 0:
// Set Datasource for Third Choice.... and so on...
break;
default:
// Default Behavior
break;
}
Hope You Get My Point.
If it was me I would use notifications instead of delegates. i.e Detect the segmentedControl value change using valueChanged inside the detailViewController and then post an NSNotification from your detailViewcontroller which your masterViewController receives.
Easiest way is:
[[NSNotificationCenter defaultCenter] postNotificationName:#"segmentOneChosen" object:nil userInfo:nil];
Have you masterViewController register for this notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(segmentOneChosen) name:#"segmentOneChosen" object:nil];
It would be better still to pass the chosen value along with the notification so you don't need a separate notification for each segment.
!! untested code:
NSArray *keys = [NSArray arrayWithObjects:#"segmentChosen", nil];
NSArray *objects = [NSArray arrayWithObjects:[NSNumber numberWithInt:self.topicsChoiceSegControl.selectedSegmentIndex], nil];
NSDictionary * dict = [NSDictionary dictionaryWithObjects:objects forKeys:keys];
[[NSNotificationCenter defaultCenter] postNotificationName:#"segmentChosen" object:nil userInfo:dict];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(segmentChosen:) name:#"segmentChosen" object:nil];
-(void) segmentChosen:(NSNotification *)notification {
NSNumber *segmentChosenNum = [[notification userInfo] valueForKey:#"segmentChosen"];
}
I know this doesn't answer your question per se, but it does provide an alternative solution to the problem you are trying to overcome.