How to call a ViewController from a UITableViewCell class - swift

I have a UITableView Class which controls the items in my custom cell. With one of these items I need to respond by moving to my LoginView. I set up a UITapGestureRecognizer with the selector called priceClick(). In this method I need to call buildNewLoginView but obviously can't use a segue so I call a class in my MenuViewController to show LoginView.
func priceClick() {
MenuViewController.buildNewLoginView(MenuViewController)
}
This just throws an error
'Expression resolves to an used function'
What can I change so that I can call this method?

Both replies helped to fix my problem but the reply from Abinav was the solution I used and it worked perfectly by using a Protocol to declare the delegate which was assigned to my cell in cellForRowAtIndexPath.

Related

Cannot add any UIView to my main UIViewController, after dismissing another UIViewController

I have a main ViewController that eventually creates and presents a new ViewController, which is used to gather some user data. When the data is picked, I dismiss the second ViewController and use the completion field of the func "dismissViewControllerAnimated" to call a method from a delegate that lets me hand in the data to the first and main ViewController (it is important to say that this method, actually sends an instance of a class with the data, created and initialized just before calling "dismissViewControllerAnimated").
At the first and main ViewController, I have defined the delegate method to do the following:
· print the data fields of the received instance, to make sure I have my data on the ViewController I want.
· Call a method from the received instance's class, that creates and initializes a UIlabel with the data just printed in the previos step (this label is stored into one of this ViewController properties).
· self.view.addSubview(/* property of UILabel type, now filled with my own UILabel */)
Everything compiles and goes perfect till the addSubview part... I also print the property once is filled, to make sure it receives the correct data, and it does. But no UILabel appears on my first ViewController View. Also, I have made many tests through other methods, and after dismissing the second ViewController, no UIView of any kind is successfully displayed on the screen.
I have also tried to use self.view.reloadInputView() after addSubview, but although it compiles, it has no effect.
Could anyone offer me a clue or something that helps me know how are UIViews displayed after dismissing another ViewController? My code is written in swift.

Custom initialise subview added from storyboard

I have a subclass of UIViewController that I want to add from the storyboard.
So I'm using what seems the standard methodology:
SubViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:#"SubViewControllerID"];
[self addChildViewController:svc];
[self.view addSubview:svc.view];
Which is fine but what if I want to call a custom init method on the subview?
I can do something like:
svc = [svc initWithFoo:#"Hello"];
Which seems to have to go after the addSubview call inorder for it to work.
Is this the best way to do this?
Seems a bit unorthodox. Calling an init method on an object that has already been created seems like its no longer truly an init method.
Maybe I should call it setWithFoo: or something and not have it return anything?
SubViewController *svc = [self.storyboard instantiateViewControllerWithIdentifier:#"SubViewControllerID"];
will cause the SubViewController to be inited with it's - (id)initWithCoder:(NSCoder *)decoder {} method.
Override that method (don't forget to call super)
If you want to do additional setup to your view controller after you instantiate it form the storyboard you can create some methods in the view controller's class and call them after the instantiate method fo the storyboard.
But be careful, if you try to make changes on any UI component in those methods, they wont be applied, and probably the app will crash. So use those methods to set params to the View Controller like array of objects, or any kind of data, and apply the UI changes for the view controller's view in viewDidLoad/viewWillAppear/viewDidAppear methods of your view controller.
Essentially I think the answer is that you can't use custom initialisers on ViewControllers added from the storyboard. Instead you have to set properties directly or through a method at the appropriate time in the life cycle as stated above.
Also as mentioned, the VC will be instantiated through initWithCoder, so calling an additional initialiser might be superfluous(?).
I encountered problems trying to use a custom initialiser that contains a call to super if I called it before the subview was added. I would just get a blank view added, I think because the superclass doesn't seem to know about the storyboard at that point. I had more success removing the call to super but that seems wrong.
This case would be more pertinent when adding subviews to a scrollview. For simplicity I left this out of my example.

How can you tell an UIViewController that a button is pressed in an UIPopOverController?

i think the title is self explaining.
I have an UIPopOverController, in it is a tableview and when i select a cell, i want to tell it the UIViewController.
Is there an easy solution or do i need KeyValueObserving or notifications?
Post an NSNotification from the tableview and add the UIViewController as an observer.
you could move with one of two approaches as per your choice.
first : using delegate/protocol.
http://www.thepensiveprogrammer.com/2010/05/objective-c-protocols-and-delegates.html
second: Set your UIViewController as the target for your UIButton.
for example
[btn addTarget:myController action:#selector(ActionWillBePerformedInController:) forControlEvents:UIControlEventTouchUpInside];
Posting an NSNotification will work well... you can also create a callback object and selector in your UITableViewController class.
you could initialize your UITableViewController with a callback object and callback selector
initWithTarget:(id)theTarget andSelector:(SEL) theSelector
...save off the values to properties
then from the didSelectRowAtIndexPath in your tableView... call
[self.target performSelector:self.selector];
using this methodology, you can define your own callback methods as you wish.. from your ViewController class that created the popover, you could do something like this...
[[MyTableView alloc] initWithTarget:(self) andSelector:#selector(popoverControllerDidRequestClose)];

Can i call a method inside a class(subclass of UIView) from another class?

Its kinda weird but i am not able to call any method defined inside the class (having the drawRect method).
This is what i do inside a UIviewcontroller class with nib for UI:
-(void)viewDidLoad {
[self.lineVar Start];
}
lineVar is an object of the class (which is a subclass of UIview) which has the function Start.
-(void)Start {
NSLog(#"Hi...");
}
I never see the log message.
I have assigned the Class with Start method to a view which occupies some part of the screen with the UIviewcontroller class with the nib file.
I want a method to be called everytime the view (subclass of uiview) is visited.
It only has awakeFromNib which is called just the first time. viewdidLoad would have done the trick but its not there by default.
Thanks
Make sure self.lineVar isn't nil when you call Start. If it's failing silently, that's probably the issue. You may have forgotten to connect the lineVar outlet in your NIB.

UITableView and didSelectRowAtIndexPath issue

I have a UITableView populated with a location-based datasource. I'm calling [self updateView]; to manually refresh the view after the location is found which works fine...but for some reason the didSelectRowAtIndexPath method isn't getting called. Any ideas of why it's not working?
Code snippet: http://pastie.org/464300
Ensure that the delegate property of your tableView is set.
slf said:
Make sure it's defined in your .h
file. The protocol does a
'doesRespondToSelector' first and if
you aren't making it public through
your header the message my fail
-respondsToSelector: will detect methods that are not exposed in the interface.