I have a ViewController with a UITextField. The user enters a string of characters into the textField, clicks a done button and then this value should be passed to a method in the entity class. I know I need to pass this value as a parameter but I'm getting stuck.
I've tried to pass this value by adding a parameter to the end of the clickDone IBAction for the done button with no luck. Thank you for even the smallest bit of help or point into the right direction.
- (IBAction)doneClicked:(NSString*) name {
...
}
I've tried creating an ivar for ViewController in my entity class h file but Xcode asks "Unknown type name 'ViewController'; did you mean UIViewController?
#interface EntityClass : NSManagedObject{
ViewController* refVC;
UITextField* textBox;
}
and then use
self.name = self.refVC.textBox.text;
in my method in entity class m file to return the NSString self.name
Don't pass the ViewController to the Entity Object. Instead, update the entity object's property in your doneClicked action.
- (IBAction)doneClicked:(NSString *)name {
myObject.name = name;
}
I figured it out. This is my final code. It works. Help is no longer needed.
Thanks!
- (IBAction)doneClicked:(NSString*) name {
...
superClass.name = headingText.text; //headingText is UITextField on ViewController
[superClass doSomething];
...
}
Related
I'm trying to play a little bit with swift and iOS 8.
The question is, I'm trying to create this setter in a view controller:
- (void)setViewController:(UIViewController *)viewController
{
_viewController = viewController;
method do something
}
This code below is in Objective-C so I'd like to create it in swift.
I think, I should use willSet, but I know how do it if it's defined when you have your own variable, how can I define that willSet method if that property is a ViewController property.
Thanks
Something like this:
var viewController: UIViewController? {
willSet {
//method to do something
}
}
You can actually access what viewController will be set to with the variable "newValue" if you need that in the method call.
Note: I made the property optional here just to get rid of compiler warnings about needing initialization
An alternate way different from #Eric's answer is by using a computed property, which mimics objective-c properties more closely, consisting on defining a pseudo-private data member and a computed property implementing a getter and a setter:
class MyClass {
var _viewController: UIViewController?
var viewController : UIViewController? {
get {
return self._viewController
}
set {
self._viewController = newValue
// Do something else
}
}
}
I defined the property pseudo-private because swift doesn't have access modifiers (yet), so everything is public - but when I see an underscore prefixing a variable or property name, that to me means private (It's a convention I use it a lot in javascript for instance).
I have method in ViewController:
(void) writeInViewController:(NSString *)var {
NSLog(#"Var: %#", var);
}
I send value to this method(writeInViewController) from another class it's receive the value and print it in the Debug Area but I can't write to the User Interface (textfield.text = var)
I need to add text to the UITextField from this method
Thanks
If i am not wrong your value getting null when you are assign to UITextField, do one thing create one instance variable or property then assign that value to variable then assign to UITextField it will work.
I am trying to pass a reference of my current NSObject Class through two other object classes so I can access the current initialization of the original NSObject class I called from.
I will try to outline why I am doing this in as simply as possible. I have 3 NSObject Classes and an appDelegate.
AppDelegate
RemoteSites
EngineRequest
EngineReasponse
This is the logical flow of the app as it stands
Appdelegate.m
calls RemoteSites method "GetRemoteSites" this method reutrns a BOOL for confirmation
RemoteSites.m
-(BOOL)GetRemoteSites {
// calls EngineRequests method like so
EngineRequests *engineRequests = [[EngineRequests alloc] init];
[engineRequests GetRemoteSites:self];
//..
}
EngineRequests.m
- (void)GetRemoteSites:(NSObject *)myObjectClass {
// get everything ready to send off request
}
send off request then return recived data + NSObject refrence to EngineReasponse
EngineReasponse.m
- (void)GetRemoteSites:(NSData *)receivedData Object:(NSObject *)requestingClass
{
// pass requestingClass to a NSObject var that will later be used to pass the data back to the original class that started the request
requestingClassObject = requestingClass
}
//..
[requestingClassObject GetRemoteSitesNow:reducedDataPacket]; // GetremoteSitesNow is a method inside RemoteSites class, however using requestingClassObject I cannot see any of the classMethods my class has in it
//..
So thats the overall flow of the process I am trying to complete, the whole point is to try and get -(BOOL)GetRemoteSites to return Yes to the AppDelegate.
In summery my question stands as this. Why can I not access RemoteSites methods from EngineReasponse's, I have passed the class Object refrence correctly I think but for some reason I cannot access the methods.
Any help solving my issue would be greatly appreciated.
EngineRequests.m
- (void)GetRemoteSites:(id)remoteSites {
// create your class object here or globally.
RemoteSites *remotesite = (RemoteSites*)remoteSites
}
EngineReasponse.m
- (void)GetRemoteSites:(NSData *)receivedData Object:(id)requestingClass
{
RemoteSites *requestingClassObject = (RemoteSites*)requestingClass
}
//
[requestingClassObject GetRemoteSitesNow:reducedDataPacket];
//
Sorry for the typo. Hope it will help.
I've added a view controller as child like this:
UIViewController *sendViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"send"];
[self addChildViewController:sendViewController];
In the sendViewController I got this property:
#property (strong, nonatomic) IBOutlet StringInputTableViewCell *nameCell;
and in that class I can do something like self.nameCell.textField.text = #"test"; to set the textField to test
But how can I get this text thats filled in the textField in the parent class?
You are mixing model and view, thus breaking the MVC design pattern.
You should not try to read what is the content of a UI element. Rather you should have all the data (i.e. model) and the view (i.e. the UI, such as text fields) managed by a controller.
There are (easy) ways to get to this information, but I strongly advise you not to go down that road!
Basic inheritance between the parent and child class should allow you to pass the property forward.
You'll need to create a child object of the class say obj. Then to get the text value of the field you'll use (in the parent class)
id obj = [[ChildClassName alloc] init];
NSString *myChildsText = obj.nameCell.textField.text; // will get the value #"test" as assigned in the childclass.
Or of course, you can create a getter and setter in the Child Class for your #property. For example ::
- (IBOutlet)nameCell {
// returns the value
}
- (IBOutlet)setNameCell :(StringInputTableViewCell)newValue {
//set the value to the #synth value hereā¦
}
then you can call the child objects getters/setters as below ::
NSString *text = [obj nameCell]; //etc etc
You can use 4 approaches in here
a) keep reference to you childviewcontroller -> get the value directly
b) use delegate method ( best for observing changes in your textfield )
c) send NSNotification -> this comes with overhead, well at certain points it can help you a lot
d) KVO
I am having trouble using a protocol to get some data from another class. I can't see how to set the delegate in a class that doesn't segue to the MVC that needs the data. I create the protocol in the MVC and implement the method(s) in some arbitrary class that contains the data I need. But I can't see how to refer back to the delegator MVC to set the delegate if there is no reference to the delegator MVC, like when you use segue.destinationViewController.
If MyViewController can create the instance of SomeDataClass, then you set the delegate there. If there is no connection between the controllers, then you might use an NSNotification instead. That is a completely anonymous way to connect instances -- you send out a notification, and any class that registers for that notification can get it.
Something like this?
#implementation MyViewController {
// keep a pointer to the data supplier class as long as this object exists
// so that it will continue to exist and send me delegate callbacks
SomeDataClass *myInstanceOfSomeDataClass; // instance variable to point to my data supplier
}
// ...
- (void)updateMyView {
if (myInstanceOfSomeDataClass == NULL) // I haven't created an instance yet
myInstanceOfSomeDataClass = [[SomeDataClass alloc] init];
SomeType *results;
if (instantResultsAreAvailable)
results = [myInstanceOfSomeDataClass getResults];
if (resultsAreOnlyAvailableFromDelegateCallback)
myInstanceOfSomeDataClass.delegate = self;
}
- (void) delegateCallbackMethod {
//...
}
#end
you want to pass data from one from viewcontroller to another class? just go through this Passing Data between View Controllers