How to call a sub view from another sub view in xcode - xcode

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];
}

Related

iOS8 : When Keyboard appear and press Back button, at that time next view appear very slow

I have ViewController (v2) with the UITextView . I pushed that view from the viewController (V1).
On V2 When I tap on textview and keyboard appear after that tap on back button and move on V1.
I repeat this process 15 to 20 time and notice that my app's performance become very slow.
Issue is Keyboard take long time to disappear when I tap on Back button :
I am using following line of code :
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(willShowKeyboard:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
}
- (IBAction)back:(id)sender
{
[self.navigationController popViewControllerAnimated:NO];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[noteView becomeFirstResponder];
}
- (void)viewWillDisappear:(BOOL)animated {
[noteView resignFirstResponder];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
[super viewWillDisappear:animated];
}
- (void)willShowKeyboard:(NSNotification *)notification
{
[UIView setAnimationsEnabled:NO];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
[UIView setAnimationsEnabled:NO];
}
- (void)keyboardDidHide:(NSNotification *)notification
{
[UIView setAnimationsEnabled:NO];
}
It is simple one line code to dismiss keyboard when user press back button
- (IBAction)back:(id)sender
{
[self.view endEditing:YES];
[self.navigationController popViewControllerAnimated:NO];
}

Is there a change to the behaviour of UIKeyboardWillShowNotification in iOS 8?

I have had a simple UIView block animation that handles animating a group of text fields into view when the keyboard will show (and animate them back when the keyboard hides). This has worked fine in iOS 6 & 7, but now I'm getting incorrect behavior, and it all points to some change in UIKeyboardWillShowNotification.
I set up an isolated project to test this further on a single text field, with two buttons that call exactly the same methods that are fired for the keyboard's WillShow and WillHide notifications. See the results in this video:
Video example
This seems like a bug to me, or it might be a change to the behavior of this notification. Does anyone know if this is intended and/or what can be done about it?
Here is the relevant code:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}
- (void)keyboardWillShow:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
self.textField.frame = CGRectOffset(self.textField.frame, 0.f, _destY - _origY);
}];
}
- (void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
self.textField.frame = CGRectOffset(self.textField.frame, 0.f, _origY - _destY);
}];
}

How to disable MasterView when the keyboard appears in the DetailView

I would like to know if it's possible (and how) when the keyboard appears in the DetailView, to disable any MasterView controls until it disappears. All of this happens in a split view based app of course.
---Update for Prince's solution---
MasterViewController.h
#property (strong, nonatomic) UIView *MasterView;
MasterViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MasterView=self.view;
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
self.detailViewController = (DetailViewController *)[[self.splitViewController.viewControllers lastObject] topViewController];
}
DetailViewController.m
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
((MasterViewController *)self.parentViewController).MasterView.userInteractionEnabled=NO;
return YES;
}
This code as is, crashes the app with an "Unknown Selector" error.
How do i bind delegates; Don't know if that's the problem or not. Any help?
Use UITextField's delegate and also bind delegates:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
MasterView.userInteractionEnabled = NO;
.......
return YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
MasterView.userInteractionEnabled = YES;
[textField resignFirstResponder];
return YES;
}
I found out a solution!
in MasterView viewDidLoad:
//---registers the notifications for keyboard---
// to see if keyboard is shown / not shown
[[NSNotificationCenter defaultCenter]
addObserver: self
selector:#selector(keyboardDidShow:)
name:UIKeyboardDidShowNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:#selector(keyboardDidHide:)
name:UIKeyboardDidHideNotification
object:nil];
and then...:
//----------Handling Keyboard Appearence---
-(void) keyboardDidShow:(NSNotification *) notification {
[self.tableView setUserInteractionEnabled:NO];
}
//---when the keyboard disappears---
-(void) keyboardDidHide:(NSNotification *) notification {
[self.tableView setUserInteractionEnabled:YES];
}
//---before the View window disappear---
-(void) viewWillDisappear:(BOOL)animated {
//---removes the notifications for keyboard---
[[NSNotificationCenter defaultCenter]
removeObserver: self
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:UIKeyboardWillHideNotification
object:nil];
}

UIAlertview in 2 view

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:

Drawing NSControl when the windows is key or not

I have a NSControl subview and I want to change the drawing when the control is not on a keyWindow. The problem is that I don't see any property that reflects that state (tried enabled property but that was not it).
In simple terms, can I differentiate between these two states?
You can use NSWindow's keyWindow property, and if you want to check to see if your control is the first responder for keyboard events also test [[self window] firstResponder] == self. I don't believe keyWindow supports KVO, but there is a NSWindowDidBecomeKeyNotification and NSWindowDidResignKeyNotification you can listen for. For instance,
- (id)initWithFrame:(NSRect)frameRect;
{
if ( self = [super initWithFrame:frameRect] )
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(display) name:NSWindowDidResignKeyNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(display) name:NSWindowDidBecomeKeyNotification object:nil];
}
return self;
}
- (void)drawRect:(NSRect)aRect;
{
if ( [[self window] isKeyWindow] )
{
// one way...
}
else
{
// another way!
}
}
- (void)dealloc;
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResignKeyNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidBecomeKeyNotification object:nil];
[super dealloc];
}

Resources