iphone - how to communicate with labels (navigation based app with storyboard) - iphone

I am making an app using a Navigation Controller, which I've set up like this:
0-Navigation Controller -> 1-View Controller (Root View)
1-View Controller (Root View) -> 2-View Controller (with class SubViewController) #1
1-View Controller (Root View) -> 2-View Controller (with class SubViewController) #2
1-View Controller (Root View) -> 2-View Controller (with class SubViewController) #3
1-View Controller contains a Text Field, UILabel, and 3 Buttons:
button #1 goes to 2-ViewController #1, #2 to #2, #3 to #3, using segue's (push) for the connections between the buttons and viewcontrollers.
If this isnt clear, I'll explain: If you press button #1 in 1-ViewController, you go to SubViewController #1.
Now where I'm getting stuck:
I want to communicate with a label in the SubViewController, but I can't draw a blue line (CTRL drag) from that UILabel to anywhere... (except the ViewController, which then gives the option: view, which doesn't seem what I need, I think).
What I want to happen:
The user enters data in the textfield in 1-ViewController, which then is displayed in the UILabel in the chosen 2-SubViewController.
How can I do this?
EDIT:
It seems that my Assistant Editor was set to Manual instead of Automatic. After switching back, I was able of dragging lines.

You'll have to do it manually:
add an IBOutlet for the UILabel in the UIViewController that should handle the label:
#interface MyViewController : UIViewController
{
IBOutlet UILabel *artistLabel;
IBOutlet UILabel *titleLabel;
}
#property (nonatomic, readonly) IBOutlet UILabel *artistLabel;
#property (nonatomic, readonly) IBOutlet UILabel *titleLabel;
you'll now have the option in Interface Builder to drag from your label to the corresponding UIViewController.

Related

Assign own View Classes to Storyboard

See this storyboard containing a Navigation View, Gestures, and a few Views.
Also within is a TabView, which contains an UIImageView and UITableViewController.
To apply my data to this TableView, I need to assign a Controller by using msExampleTableViewController.
The problem: the input/dropdown field removes entry after pressing Enter (like if this class is not assignable).
What is wrong here, and how can it best be done?
NA UITableViewController does assume that the root view will be a UITableView. When you combines UITableView with others controllers.. you can do in your Controller (ViewController in your case):
#interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource>
#property (nonatomic, assign) IBOutlet UITableView* tableView;
In Interface Builder set both the delegate and dataSource outlets of the table view to be equal to its superview's view controller (File's Owner)...
Now, Your view controller implementation should be the typical TableViewController implementation: cellForRowAtIndexPath, etc.

How to pass a value to parent view controller? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
dismissModalViewController AND pass data back
I'm new to ios development and stuck on this problem:
I'm using storyboarding and have a navigation controller, vcA, with a TableView in it which shows some data from a MutableArray (which is initialized in viewdidload of the same class). After selecting any cell, a second view controller, vcB, is shown with a TextField in it and a button called "Add to list".
What I want is that when I enter some text in the TextField and press the "Add to list" button the text should be added to the array of previous view (which gets shown in the TableView) and when i tap the "Back" button on vcB's navigation bar, vcA should show the updated TableView with the new entry in it (on the top of list). Basically I want to add the text from vcB's TextField to the array of vcA and show the new array after clicking the BACK button.
I have searched a lot about this issue and seem to find that delegate and protocols is the way to achieve the desired result but I'm having trouble understanding delegation.
I have the second view controller presenting itself as modal in this example:
In the second view controllers h file:
#protocol SecondViewControllerDelegate <NSObject>
- (void)addItemViewController:(id)controller didFinishEnteringItem:(NSString *)item;
#end
#interface SecondPageViewController : UIViewController <UITextViewDelegate>
{
NSString *previouslyTypedInformation;
}
#property (weak, nonatomic) IBOutlet UITextView *textView;
#property (nonatomic) NSString *previouslyTypedInformation;
#property (nonatomic, weak) id <SecondViewControllerDelegate> delegate;
In the second view controllers m file make sure to synthesize properties and add then add this:
- (IBAction)done:(id)sender
{
NSString *itemToPassBack = self.textView.text;
NSLog(#"returning: %#",itemToPassBack);
[self.delegate addItemViewController:self didFinishEnteringItem:itemToPassBack];
//dismiss modal view controller here
}
Then in the first view controllers h file set it as delegate:
#interface FirstPageViewController: UIViewController <SecondViewControllerDelegate>
#property (nonatomic) NSString *returnedItem;
Then in the first view controller's m file synthesize and add the method:
- (void)addItemViewController:(SecondPageViewController *)controller didFinishEnteringItem: (NSString *)item
{
//using delegate method, get data back from second page view controller and set it to property declared in here
NSLog(#"This was returned from secondPageViewController: %#",item);
self.returnedItem=item;
//add item to array here and call reload
}
Now you have the text of what was returned! You can add the string to your array in the first view controller's viewDidLoad and call
[self.tableView reloadData];
and it should work.

override class or subclassing in storyboard

I'm having issues assigning a custom class in storyboard.
In past projects I have overridden/subclassed UIPageControl to a custom class (ZZPageControl) by adding the .h to the view with the UIPageControl.
#import "MyPageControl.h"
#interface ScrollerVC : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollView;
IBOutlet MyPageControl *pageControl;
NSMutableArray *viewControllers;
BOOL pageControlUsed;
}
Then in IB I assign my custom class instead of UIPageControl.
Except my new project uses Storyboard rather than separate XIBs. When selecting the UIPageControl in the view and checking its class in the Identity Inspector only UIPageControl is listed. If I type MyPageControl is doesn't stick.
I've tried adding #import "MyPageControl.h" in the app delegate, in the view that initiates the UINavigation control, in the .pch file. I can't get my custom class to show up in the Storyboard.
How can I override the UIPageControl in the StoryBoard view?

Custom Control in iPhone Interface Builder

This appears to be a common question, but I haven't seen it answered clearly or succinctly - and as such I'm having trouble making it work.
I have created a Widget.xib file in Interface Builder. It consists of:
View (UIView)
ImageView (UIImageView)
Button (UIButton)
Label (UILabel)
I have created Widget.h and Widget.m, which implement class Widget, which derives from UIView. In Interface Builder, Widget.xib has its File's Owner set to the Widget class.
Widget.h looks like this:
#interface IconView : UIView {
IBOutlet UIButton *widgetButton;
IBOutlet UILabel *widgetLabel;
IBOutlet UIView *widgetView;
IBOutlet UIImageView *widgetImage;
}
#property (nonatomic, retain) IBOutlet UIButton *widgetButton;
#property (nonatomic, retain) IBOutlet UILabel *widgetLabel;
#property (nonatomic, retain) IBOutlet UIView *widgetView;
#property (nonatomic, retain) IBOutlet UIImageView *widgetImage;
Widget.h includes:
#synthesize widgetButton, widgetLabel, widgetView, widgetImage;
And its dealloc releases those four objects. In Interface Builder, the button, label, view, and imageview are all connected to the appropriate IBOutlets.
Back in Interface Builder, I've opened another XIB from the project. This is Playfield.xib. It's a more traditional view, backed with PlayfieldViewController.h and .m. It contains:
View (UIView)
ImageView (with a static graphic) (UIImageView)
Button (UIButton)
Widget
Widget
Widget
In Playfield.h, I've created IBOutlets for the button and the three Widgets.
#interface LinkViewController : UIViewController {
IBOutlet UIButton *buttonBack;
IBOutlet Widget *widget0;
IBOutlet Widget *widget1;
IBOutlet Widget *widget2;
}
#property (nonatomic, retain) IBOutlet Widget *widget0;
#property (nonatomic, retain) IBOutlet Widget *widget1;
#property (nonatomic, retain) IBOutlet Widget *widget2;
- (IBAction)buttonBackClicked:(id)sender;
These are also synthesized and released in Playfield.h. In Interface Builder, Playfield.xib shows three empty rectangles where the widgets have been placed, which is what I expected. Those are wired up to their corresponding IBOutlets in Playfield.h.
So, at this point I assume everything is ready to go. In Playfield.m's viewDidLoad, I do this:
self.widget0.label.text = #"Feed Me";
In my mind, that references the widget0 IBOutlet declared in Playfield.h. That connects to the first Widget item in Playfield.xib. That, in turn, has a declared label IBOutlet in Widget.xib and Widget.h. A UILabel has a Text property. So it should change the text on the label within the widget.
But it doesn't. What am I doing wrong?
(I want to emphasize that I'm not looking to make an Interface Builder plugin. I know those are complicated. Within IB, I'm fine manipulating my Widgets as empty rectangles. I'll also note that I've set the background color for the widget's View, in Widget.xib, to white. When I run the project, I can clearly see the white rectangles atop the playfield graphic, so I know the widgets are "in" there. I just can't seem to access them.)
Where did you call the loadNibNamed:owner:options: method to load your Widget subviews from your Widget.XIB?
Also you did connect your views in your Widget.XIB file to the File's Owner (which is a Widget object) so the IBOutlets are connected and give you access to the views… but did you add the views as a subview to your Widgets?
Is the outlet nil when you try to access it using self.widget0.label ? Check if self.widget0 is not nil (I guess for that it is OK) but also if self.widget0.label is not nil too (I guess that's the real the problem). If it is, you probably forgot to load your Widget.xib for each widget view. If it isn't, then it probably means that you have your label, but it isn't added to the view hierarchy.
Your description of your XIB setup doesn’t mention the label outlet on Widget being actually connected to its label in the XIB. Check that, maybe? It sounds as if you’ve got everything else set up, though you don’t need to declare the IBOutlet attribute on the instance variable, just the property.
A common one I often miss is setting the class type to widget in IB.

Navigation Within TabController

I am trying to use UITabController as may controller in my main window and add navigation controllers to some tab bar items.
For example, the first tab has a navigation controller with table view:
alt text http://www.freeimagehosting.net/uploads/f3ad987c86.png
The SettingsViewController is associated with its own NIB file, where a table view is defined. Within that xib file, I have a table view and set it to the outlet of SettingsViewController class property myTableView.
Here are my h files:
// header file for SettingViewController class
#interface SettingsViewController :
UIViewController <UITableViewDelegate, UITableViewDataSource> {
UITableView *myTableView;
// other codes vars
}
#property (nonatomic, retain) IBOutlet UITableView *myTableView;
// ...
#end
// header for main app delegate
#interface MainAppDelegate :
NSObject <UIApplicationDelegate, UITabBarControllerDelegate> {
UIWindow *window;
UITabBarController *tabBarController;
// ...
}
#property (nonatomic, retain) IBOutlet UITabBarController *tabBarController;
// ...
#end
In my SettingsViewController.xib file, through IB, I linked outlet myTableView to the xib's file owner, ie, SettingViewController class:
alt text http://www.freeimagehosting.net/uploads/e577d35137.png
The problem is that in the main xib file, for the SettingsViewController navigation, there is one outlet myTableView. I am not sure if I have to set this to somewhere?
The exception I get is "[UIViewController _loadViewFromNibNamed:bundle:] loaded the "SettingsViewController" nib but the view outlet was not set."
SettingsViewController already has a view property. Are you sure this one is hooked up in Interface Builder? (You probably want it to be hooked up to your UITableView.)