I'm new to Objective C and have a pretty basic question. So I have 2 variables (IBOutlets that are UITextFields) in one UIViewController. After the user enters text into those UITextFields, s/he proceeds to a new viewcontroller. When the user enters viewcontroller #2, I want to insert the values of the IBOutlets from viewcontroller #1 into an NSString I preset. For example, 1 variable is a name and one is an interest. The NSString should read "Hello [name], thanks for your interest in [interest]. We appreciate it." So how do I pass the IBOutlets from viewcontroller 1 to viewcontroller 2, and insert them as NSStrings into the NSString that isn't variable (the thanks for your interest, etc. etc. part). I appreciate any help you can provide because I'm a total Objective C newbie. Thanks for taking the time to read this.
-Reynold
This is really a two part question. To put those strings together the way you want to, I'd recommend: [NSString stringWithFormat:FORMAT];
In your case, the implementation would probably be a little like this:
NSString *name = nameTextField.text;
NSString *interest = interestTextField.text;
NSString *resultString = [NSString stringWithFormat:#"Hello %#, thanks for your interest in %#. We appreciate it", name, interest];
Passing this value to another view controller is a little bit more complicated. I recommend creating a property in the second view controller like so:
#interface ViewController2 : UIViewController{
NSString *myString;
}
#property(nonatomic, retain) NSString *myString;
#end
Then, when you set up the new view controller you can configure it like this:
UIViewController *vc = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
vc.myString = resultString;
[self.view addSubview:vc.view];
[vc release];
Good luck.
Related
I am building an iOS7 app and I am trying to make use of the new useLayoutToLayoutNavigationTransitions effect. I have 2 UICollectionViewControllers and when I do the following I get the transition effect
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondVC.useLayoutToLayoutNavigationTransitions = YES;
[self.navigationController pushViewController:secondVC animated:YES];
this works fine but what I want to do is make an api call and then in the completion block I want to push onto the nav stack like so
[webAPI getDetailsWithParams:params andCompletionBlock:^(id dict) {
//some work on the result
SecondViewController *secondVC = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
secondVC.useLayoutToLayoutNavigationTransitions = YES;
[self.navigationController pushViewController:secondVC animated:YES];
} andErrorBlock:^(NSError *error) {
}];
but this crashes every time with the following msg
-[UICollectionView _invalidateLayoutWithContext:]: message sent to deallocated instance 0x17a26400
can anyone tell me what I am doing wrong in this case? How can I get the transition effect when pushing from completion block?
EDIT: by changing it to the following I was able to transition to the second viewcontroller.
MyLayout *layout = [[MyLayout alloc] init];
SecondViewController *expandedVC = [[SecondViewController alloc] initWithCollectionViewLayout:layout];
and I also deleted the nib file that went with the file. nib file just consisted of a collection view and it was the file owners view.
While I can now transition I still do not understand why I could not do the previous nib method with in a block. So I would be grateful if someone could shed some light on it for me.
In order to use UICollectionViewController's useLayoutToLayoutNavigationTransitions to make transitions, the layout of the SecondViewController must be known. However, if you use initWithNibName:bundle: initializer, layout is not internally prepared yet, making your desired transitions impossible. As you mentioned in your edited question, you have to use [UICollectionViewController initWithCollectionViewLayout:] to initialize your second UICollectionViewController. Since your xib file has the same name as your class name, SecondViewController.xib is going to be loaded automatically by UIViewController, superclass of UICollectionViewController.
I think you were making UIKit calls from a thread that wasn't the main thread
I have an NSString called stringToDisplay, in which i would like to assign a different value if a certain ViewController was visited. How could I set this up? Is this even possible? ...example of what I want:
If ([prevViewController.visited] == Yes)
{
stringToDisplay = value2;
}
Assuming you are using a navigation controller, you can just do:
NSArray *views = self.navigationController.viewControllers;
UIViewController *previous = [views objectAtIndex:[views count]-2];
where [views objectAtIndex:[views count]-1] is the current ViewController;
I am using ARC with TWRequest. I have successfully returned a search from twitter and created an array of the results. Here is my code...
NSArray *results = [dict objectForKey:#"results"];
//Loop through the results
NSMutableArray *twitterText = [[NSMutableArray alloc] init];
for (NSDictionary *tweet in results)
{
// Get the tweet
NSString *twittext = [tweet objectForKey:#"text"];
// Save the tweet to the twitterText array
[twitterText addObject:twittext];
}
NSLog(#"MY ************************TWITTERTEXT************** %#", twitterText ); </p>
My question is, I want to use twitterText later in the .m file under cellForRowAtIndexPath but as soon as the loop through (as above) is finished it is being released under ARC. I have set the property as strong in my .h file (as well as declaring it above just prior to the loop). Printing a log straight after the loop through as above prints the twitterText Array fine but the same Log in cellForRowAtIndex path method returns a blank. Any help would be appreciated. Thanks. Alan
Alan,
You do not appear to be saving twitterText in any permanent ivar. As the code is written above, ARC properly deallocates twitterText after the log statement.
Hence, just assign twitterText to an ivar in your controller.
Andrew
I have a pickerView in a popOver.
When the user selects a row from the pickerView, it should be stored into a UITextField (subject)
The method i'm having troubles is:
- (void) viewWithPickerController:(ViewWithPickerController*) viewWithPickerController didSelectValue:(NSString*) value
{ NSLog(#"selected value is: %#",value);
subject.text = [NSString stringWithFormat:#"%#",value];
NSLog(#"subject is: %#",subject.text);
}
Where value is the row of the pickerView: it logs the right value but it doesn't set it to the textField subject, which seems to be null.
Where am i wrong?
You need to make sure your "subject" IBOutlet is set.
If you're doing this picker view method before your XIB / view controller is instantiated, that would explain why subject is a nil object.
How else would you be instantiating "subject"?
I search Google to find a good answer but the most tutorials are shown in previous Xcode Versions...
Also, I don't want to drag-n-drop cells from the Interface Builder, but to control the Table View programmatically (from an NSObject subclass file).
What I currently do is this: 1. Create a file named tableController.h that is a subclass of NSObject.
2. I create an NSObject Object in my Nib File (and set it as a subclass of tableController).
3. I drag a Table View to my window.
4. I CTRL+Drag from the Table View to my tableController.h so to create the outlet "tableView"
5. I create these functions in the interface file:
-(int)numberOfRowsInTableView:(NSTableView *)cocoaTV;
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row;
6. I implement the functions like this:
-(int)numberOfRowsInTableView:(NSTableView *)cocoaTV{
return 5;
}
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row{
NSArray *tvArray = [[NSArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
NSString *v = [tvArray objectAtIndex:row];
return v;
}
Then I CTRL+Drag from the Object in the Interface Builder to the Table View to set the dataSource and to set it as delegate.
When I build and Run the App it shows that it has created the 5 Rows but in every cell in every column it says "Table View Cell".
Any help would be appreciated....
-(id)tableView:(NSTableView *)cocoaTV:objectValueForTableCollumn:(NSTableColumn *)tableCollumn row:(int)row is wrong.. i'm not sure how it compiles, to be honest (unless there was an error copy/pasting it). the method should look like:
- (id)tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
NSArray *tvArray = [[NSArray alloc]initWithObjects:#"1",#"2",#"3",#"4",#"5", nil];
NSString *v = [tvArray objectAtIndex:row];
return v;
}