How can I reload data of UIVIewcontroller from the UIView.Actually I have a UIVIewController which has a Custom class class of UIView .Now In UIViewcontroller i have a label and i need to print values of label which is been defined in the UIView.
but viewdidload of the UIViewController is been compiled firstly then compiler moves to UIView.then how can I print or Reload the UIVIewController
. UILabel Text I have defined in the ViewDidLoad of UIViewController and values taken from the UIView class
So How can I reload the UIViewController.
what I am doing is:
UIVIewController(thirdTab) class
-(void)viewDidLoad{
[self reloadInputViews1];
}
-(void)reloadInputViews1{
appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// labelPrint.text=appDelegate.dayPrint;
[self.labelPrint setText:labelPrint.text];
}
UIView class
- (void)drawRect:(CGRect)rect {
AppDelegate* appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.dayPrint=theDate;
NSLog(#"appde%#",appDelegate.dayPrint);
[appDelegate.thirdTab reloadInputViews1];
}
But this does not work ...I am not able to print values in the label.how to do that..to get values from the UIViews
You can call setNeedsDisplay on your view to force it to be redrawn. But I am not sure that this is the best solution, in fact, instead of calling setNeedsDisplay on your view, you could call directly reloadInputViews1 on your controller and it would be the same (actually, it would be much better).
Apart from this, I don´t think that your current design is very clean. I would suggest you to define a delegate protocol between your view and your controller to make that kind of relationship more explicit.
Related
I'm having to class one which extends NSObject and second the view controller.
What is my problem is that I want to go to NSObject to view controller that loads my xib file of that view controller.
I have used the traditional, with no success thus far.
Suppose NSObject is MagentoLogin.m and I want to go to viewController, how can I achieve this?
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate.navigationController presentViewController:myNewViewController animated:YES completion:nil];
try this hope it will help
I have an iPhone Application with multiple view controllers , in all view controller's header is common.
I dont want to use the same method and create common view in all controllers.
So My questions is how do i use this common view in all controllers.
Use initWithNibNamed:"name of your xib" when you alloc your new view controller. It's simple.
Same but we can avoid defining a variable -
[self.view addSubview:[[(NavAppAppDelegate *)[[UIApplication sharedApplication] delegate] headerview] view]];
ok so you have to create it in Application Delegate once.
in .h
#property(nonatomic,strong) uiviewcontroller headerview;
in .m
#synthesize headerview=_headerview;
then alloc it in "didFinishLaunchingWithOptions" in appdelegate as singleton
self.headerview = [[headerview alloc] initWithNibName:#"headerview" bundle:nil];
So every time you want to add it to your view.
Create object from application delegate in your class after import it.
applicationdelegate app = [uiapplication sharedapplication]delegate];
[self.view addsubview:app.headerview.view];
I am trying to create a UITableView using storyboard but I came to something that at the end may be easy but I have no idea how to solve it.
First of all let me point out that I know that one of the limitations of storyboards is that you will have to dig through the storyboard to find information about a view you have and link it to the app delegate.
I have create my mutable array and the information that I will use in the table in the app delegate and now I want to reference that UITableView to the app delegate. The hierarchy goes like that
First I have the root view that once you click on a button it will redirect you to the second view
Inside the second view there is another button that once you press it it will redirect you to the UINavigationController
The UINavigationController contains the UITableView.
Therefore as you can see there are two views before the navigation control and the UITableView.
Here is the code I am trying to use but it does not work
UIViewController *viewController = (UIViewController *)self.window.rootviewController;
// The next line refers to the second view but does not work at all
UIViewController *secondView = [[UIViewController viewController] objectAtIndex:1];
//Then the following line is to redirect from the second view to the navigation controller
UINavigationController *navigationController =[[secondView viewController] objectAtIndex:0];
//Then is the table view
BuildingsViewController *buildingsViewController = [[navigationController viewControllers] objectAtIndex:0];
The above code does not work. Can anyone please help me?
Thanks a lot
If this code is in the app delegate there are a variety of reasons why it will probably not work. Firstly you appear to be mixing up View's, ViewControllers and Navigation controllers with what you are trying to do. Secondly there is no guarantee at the time you are trying to do this that all of the views/viewcontrollers have yet been created yet or are joined in the way they will be when the final building view controller is rendered.
What you could try instead is in your BuildingsViewController (which you say is your table view controller) you can get a handle to the App Delegate by using
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
Once you have a handle to the delegate you can simply reference your mutable array structure etc. that you created on it from within your BuildingsViewController.
e.g. in the 'numberOfRowsInSection' method:
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
NSMutableArray *myBuildings = myAppDelegate.buildingArray;
return [myBuildings count];
Or in the cellForRowAtIndexPath method:
// something like this but using your names for app delegate, building array and the accessor for the building name
MyAppDelegate *myAppDelegate = (MyAppDelegate *)[UIApplication sharedApplication].delegate
NSMutableArray *myBuildings = myAppDelegate.buildingArray;
cell.textLabel.text = [myBuildings objectAtIndex indexPath.row].theBuildingName;
I have a method "someMethod" declared in OneViewController.h
#interface OneViewController
{
UIView *tempView;
..
}
-(void) someMethod ;
#end
and implemented in OneViewController.m file
#implementation OneViewController
-(void) someMethod
{
tempView = [[UIView alloc]initWithFrame:CGRectMake(100, 50, 200, 250)];
tempView.backgroundColor = [UIColor yellowColor];
if([[self.view subviews] containsObject:tempView])
[tempView removeFromSuperView];
else
[self.view addsubview:tempView];
}
I want to call someMethod when present at different viewController - secondViewController
(something like [OneViewController someMethod]), So that when I get back to OneViewController I can see the changes made by someMethod.
Do I need to use appDelegate methods?
I have tried following but it doesn't work.
neViewController *newViewController = [[OneViewController alloc] init];
[newViewController someMethod];
Thanks for any help in advance..
In the SecondViewController, declare a reference for OneViewController class. You can have assign property. Set the reference before you move to SecondViewController. Now with the reference, you can call the instance method [_oneView someMethod].
Edit:
Declare
OneViewController *_oneView;
Also add the assign property,
#property(nonatomic,assign) OneViewController *_oneView;
Synthesize the variable in .m file.
While showing the SecondViewController from OneViewController, just add the following line.
secondView._oneView = self;
sometimes calling a method directlry creating [classObject methodName] does not refelect the changes in views. Like if you want to change a UIScrollView property from scrollEnble = NO; to scrollEnable = YES;, it does not refelect.
You should use singleton of UIApplication.
Suppose you want to call ViewController1's method - (void)myMethod in ViewController2 then here are the steps:
In you AppDelegate import ViewController1 and create its object *vc. Delare property #property (strong, nonatomic) ViewController1 *vc;. synthesize also.
Now come to Viewcontroller1 class. Import AppDelegate.h in you Viewcontroller1's and in viewDidLoad write like this:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.vc1 = self;
Go to your ViewController2.h and import AppDelegate.h
Go to the line where you want to call ViewController2's method and write like this:
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate vc1] myMethod]; // to allow scrolling
Ideally, you should create a protocol and delegate methods to accomplish what you are looking for.
Create a protocol, implement it in secondViewController and set the protocol delegate to firstViewController and then use the delegate methods for invoking the relevant methods in secondViewController
I hope it works for you..!!
One way of doing it is to change the declaration to +(void) someMethod ; in your OneViewController.h and change the minus to a plus correspondingly in the implementation file. This will make it a class-method and not an instance method. Then in your SecondViewController.m file, make sure to put #class OneViewController; before the implementation declaration; then you can call [OneViewController someMethod] and it should execute. Cheers!
This question already has answers here:
Get to UIViewController from UIView?
(29 answers)
Closed 8 years ago.
There is a way to get a view controller reference from a UIView object? I need something like this:
MyParentViewController *myParentViewController = [self.view.superview controller];
You can use the -nextResponder method to do it. According to http://developer.apple.com/library/ios/documentation/uikit/reference/UIResponder_Class/Reference/Reference.html#//apple_ref/occ/instm/UIResponder/nextResponder , "UIView implements this method by returning the UIViewController object that manages it (if it has one) or its superview (if it doesn’t)"
UIView does not have reference to UIViewController by default.You can add it to your UIView subclass and set it when you create UIView in UIViewController.
If you are looking for parent of the viewcontroller, each UIViewController has property parentViewController, but if you want to access this from UIView you need to first get to your UIViewController.
You can see example how to create reference to your UIViewController in your subclass of UIView and how/where to set it up in View Controller Programming guide for iPhone, see section Creating the View Programmatically in Defining a Custom View Controller Class, here is the example, for more details see the linked Metronome example.
- (void)loadView {
self.wantsFullScreenLayout = YES;
MetronomeView *view = [[MetronomeView alloc]
initWithFrame:[UIScreen mainScreen].applicationFrame];
view.metronomeViewController = self;
self.view = view;
self.metronomeView = view;
[view release];
}
In header:
#interface MetronomeView : UIView {
MetronomeViewController *metronomeViewController;
...
You can use
[(MyParentViewController *)[[self.view superview] nextResponder] doSomething];
You shouldn't save the reference to the view controller as it may change dynamically.
Traverse the responder chain every time you need it.
You can use the following:
UIViewController* yourViewController =
(UIViewController*)[(YourAppDelegate*)
[[UIApplication sharedApplication] delegate] viewController];