Accessing to UIScrollView from other viewController - iphone

I have two viewController:
AController (UIScrollView is subview) and BController
from AController I using presentModalViewController to BController . From BController i using dismissModalViewControllerAnimated to back to AController. But, i want set hidden for the UIScrollView from BController.
Please help me !
Thanks!

Use delegation, if you dont have so much expariance use in your AView viewWillAppear notification:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(hideScrollView) name:#"hideScrollView" object:nil];
and in your BView use:
[[NSNotificationCenter defaultCenter]
postNotificationName:#"hideScrollView" object:nil];
In result i will fire hideScrollView method in AView when you can hide your scrollView.

I think the cleanest way to do this is to use a delegation protocol. AController will be the delegate of BController. And before BController is dismissed, it can call a method of the protocol to alert AController, and AController itself while hide the scrollView.
Alternatively, you can override -viewWillAppearin the AController implementation file, to test if the 'self.presentedController' is a subclass of BController. If it is, you can hide the scrollView.
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
if ([self.presentedViewController isKindOfClass:[BController class]]) {
//hide the scroll view
}
}
Also, I would suggest that you don't use -dismissModalViewControllerAnimated: unless you want to support iOS 4 and earlier: this method has been deprecated in iOS 6 and you'd better use - dismissViewControllerAnimated:completion: right away.

Related

Call [tableView reloadData]; on a viewController from a modalViewController

I have a modalViewController that comes up over the top of a viewController with a tableView. When the user clicks a button on the modalViewController I want to reload the tableView within the viewController with this:
[tableView1 reloadData];
I do not want to put the reload in the viewDidAppear or viewWillAppear methods as they get called when i do not need the tableView to reload (i.e. when the user clicks the back button to return to the tableView).
Is there a way to do this?
Try
1) write one method which reloads the table data.
2) Call it on the back button clicked.
This is the classic delegate pattern problem, in your modal view controller you need a delegate reference to the current view controller presenting it
//Modal
#protocol ModalVCDelegate
- (void)tappedBackButton;
#end
#class ModalVC: UIViewController
#property id<ModalVCDelegate> delegate;
#end
#implementation
- (void)backButtonTapped:(id)sender
{
if (self.delegate)
[self.delegate tappedBackButton];
}
#end
Now, in your presenting VC, just process this delegate message
//Parent VC
- (void)showModal
{
ModalVC *vc = [ModalVC new];
vc.delegate = self;
//push
}
- (void)tappedBackButton
{
[self.tableView reloadData];
//close modal
}
You can use delegate . If find it more harder then alternative is to use NSNotificationCenter. You can see accepted answer for Refreshing TableView. This is really very short, easy and understandable way.
using Notification like bellow Method:-
Create NSNotificationCenter at yourViewController's ViewdidLoad Mehod
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(ReloadDataFunction:)
name:#"refresh"
object:nil];
[super viewDidLoad];
}
-(void)ReloadDataFunction:(NSNotification *)notification {
[yourTableView reloadData];
}
Now you can Call this Notification from your modelViewController BackButton or else you want from calling this Refresh notification like putting this line of code:-
[[NSNotificationCenter defaultCenter] postNotificationName:#"refresh" object:self];
NOTE: postNotificationName:#"refresh" this is a key of particular Notification
Try to use this one
Make a Button and click on this button and than you can reload your data.
This button make custom and use it on background.
- (IBAction)reloadData:(id)sender
{
[tblView reloadData];
}
You can use NSNotification to refresh table on ViewController.
Inside viewController :
-(void)dealloc{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
Write code in viewDidLoad:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(reloadMainTable:)
name:#"ReloadTable"
object:nil];
- (void) reloadMainTable:(NSNotification *) notification
{
[tableView reload];
}
Inside ModelViewController:
[[NSNotificationCenter defaultCenter]
postNotificationName:#"ReloadTable"
object:nil];
Here you can also send custom object instead of nil parameter. But be care full about removal of NSNotification observer.

UIKeyboardDidHideNotification error application crashing

I'm using a UIView. The application is using ARC. UIView is used in multiple view controllers. In UIView a listener to the UIKeyboardDidHideNotification is added. The listener works fine with some view controllers and with other view controllers it crashes the application. Especially when I use in the second view contoller after using in the first. The error is
* -[appname keyboardWillHide]: message sent to deallocated instance 0xb9c2760
In some scenarios the listener is getting called twice.
The code i have added in the uiview drawrect method is:
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
the code for listener is
-(void)keyboardWillHide
{
if(boolisViewlifted)
{
CGRect newFrame=self.frame;
newFrame=CGRectMake(self.frame.origin.x, self.frame.origin.y+250, self.frame.size.width, self.frame.size.height);
self.frame=newFrame;
boolisViewlifted=false;
}
}
The uiview appears on top of the calling view controller. Please let me know what causes this error and how to solve it.
Your view is getting unloaded because of memory warnings. You need to override dealloc method & remove observer for all notifications in all views where you added observer for notifications.
//do add in all views
-(void)dealloc
{
//[super dealloc];//As you are using ARC don't call super's dealloc
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

dismissModalViewControllerAnimated from another view controller class

I do have two ViewController class, one firstviewController other secondViewController in first viewcontroller i call this [self dimissModalViewControllerAnimation:NO];
to dimiss the view! now i need to dimiss the same view from another secondViewController class.
So do i need to call super in that!
[super dismissModalViewControllerAnimated:NO];
Or Do i need to create any protocol for dismissing the view! from another secondViewController class.
Can any guide me with this issue.
you can register a notification in firstViewController's viewDidLoad
[[NSNotificationCenter defaultCenter] addObserver:self selector:#selector(handleNotification:) name:#"MyNotification" object:nil];
Add the event handler in firstViewController
- (void)handleNotification:(NSNotification*)note {
[self dismissModalViewControllerAnimated:NO];
}
Then you can trigger the event in secondViewController
[[NSNotificationCenter defaultCenter] postNotificationName:#"MyNotification" object:nil ];
You should only be using super when you're overloading a method definition, e.g.:
- (void)viewDidLoad
{
[super viewDidLoad];
self.title = #"Login / Signup";
}
Typically, if you're trying to tell one view to do something from another view, delegates are your friend. You could create a weak delegate variable to hold a reference to the view controller to be dismissed, and call [delegate dismissModalViewControllerAnimated:NO];

Dismiss UIActionSheet before device rotate?

In portaint Actionsheet show in tabbar. In landscape - in window. I heed do remove UIActionSheet from tabbar before rotate, for after rotate show in wondow.
Inside – willRotateToInterfaceOrientation:duration dismiss action sheet dont work.
You can use NSNotification for this. Add this notifier in your viewdidload file you willl get notify when orientation is going to change:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(OrientationChanged:)
name:#"UIDeviceOrientationDidChangeNotification"
object:nil];
…add this method:
-(void)OrientationChanged
{
}
See NSNotification class reference.
Me i dismiss before rotation and show after, then the UIActionSheet is replaced at middle;
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[APP.validSave dismissWithClickedButtonIndex:1 animated:NO];
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
[APP.validSave showInView:APP.navController.view];
}

How to refresh UITableView after app comes becomes active again?

I would like my UITableView to reloadData once my app is active again, after a user exits the application. I know I need to implement (in my app delegate):
- (void)applicationDidBecomeActive:(UIApplication *)application
but im not sure how to reference the current UITableView?
UPDATE:
My UITableView is a separate controller. It is actually presented as follows
AppDelegate > Root View Controller > Pushes UITabBarController modally which has a UITableViewController
following up on Ole's answer above
add this when initializing the viewcontroller
[[NSNotificationCenter defaultCenter] addObserver:self
selector:#selector(becomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:nil];
add the actual method in the controller
- (void)becomeActive:(NSNotification *)notification {
NSLog(#"becoming active");
}
be sure to clean up the notification
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
If you can't access your view controller from the app delegate, you could also have your controller listen to the UIApplicationDidBecomeActiveNotification notification.
you can create your class called TableViewManager. in there register list of UITableView so that you can refresh any table you want.
it's like this, in yourTableViewManager class, you have a method called
- (void)RefreshTableView:(UITableView *)tableView {
if(tableView != nil)
[tableView reloadData]; }