XIB Displaying SubComponent - objective-c

I created a custom view, which has one button and one text field , as given below
#interface CommUICustomSignInView : CommUICustomView {
IBOutlet NSButton *pBtn;
IBOutlet NSTextField *pTextField;
NSTrackingArea *pTrackingArea;
NSCursor *pPonitHandCursor;
}
#property (nonatomic,retain)IBOutlet NSButton *pBtn;
#property (nonatomic,retain)IBOutlet NSTextField *pTextField;
All items are linked properly, with the view,
In Another window controller XIB, i have added one tab view, in one of the tab item view, i am going to add this view,
added one tab view and assigning this view as below,
NSTabViewItem *pTabViewItem = [pTabView tabViewItemAtIndex:0];
if(pOfflineCTlist == nil){
pOfflineCTlist = [[CommUIOfflineCTlist alloc]
initWithNibName:#"CommUIOfflineViewController" bundle:nil];
}
[pTabViewItem setView : [pOfflineCTlist view]];
[pTabView selectTabViewItemAtIndex:0];
Now with that, i could able to track the mouse event in customSignInview, in nstrackregion,
but i couldn't see the other controls, sign-in button and text field,
Am i doing something wrong,

Finally i found following way to do this,
1 -- Created a Resource like Button / text field in the Main view,
2 -- Put them over the custom view,
3 -- Link them with the custom view,
now i could able to see it,
Is there any other approach then please suggest me,

Related

Can't change UILabel's text from another View Controller

I have two view controllers, one with a text field and a 'Next' button and another one with just a label. The idea is that you fill in your name in the text field and click 'Next'. It should then switch to the other view and the label should show your name.
When I switch views however, the label is just empty. I am rather new to Objective-C and I'm hoping someone knows why this is happening :).
ViewController.m:
#interface ViewController () {
IBOutlet UILabel *label;
IBOutlet UITextField *textField;
}
-(IBAction)go:(id)sender
{
label.text = textField.text;
}
-(IBAction)remove:(id)sender {
[sender resignFirstResponder];
}
You said that you have two View Controllers. The first View Controller has an instance of UITextField, and a UIButton. Your second View Controller just has an instance of UILabel.
However, I noticed in your code that ViewController.m, which I am assuming is your first View Controller, has two IBOutlets, one for a UILabel and one for a UITextField, which doesn't make sense because the UILabel is supposed to be part of your second View Controller.
What you need to do is properly delete the IBOutlet for the UILabel from your first View Controller. Then, in your second View Controller, add the IBOutlet for it's UILabel.
Then, you need to implement the prepareForSegue method in your first View Controller and add a code statement like this:
[[segue destinationViewController]label].text = textField.text;
This way, when the segue is performed, you will successfully pass your first View Controller's text field's text to your second View Controller's label.

Open 3 views on 3 buttons

I have 3 buttons on same page but those buttons are calling 3 different views on
click.Like button1 opens view1,button2 open view2 and button3 open view3.but i want all
those buttons on upside of view like tab bar is having downside.I tried by creating or
calling views on button click but on every click it's creating new object of view which i
don't want..Please help me to solve this issue.Is there any other way to solve this
problem. I tried segment too but not working.
I'm not quite sure, if this is what you really want.
The view would be larger than it actually requires to be, regarding your screenshot?
Why don't you just set the views size to match the size of the view that is below your three buttons.
In case the entire ViewControllers' view is of height: 524px
substracting the orange bar and the buttons: around 450px
Now just create the view from your buttons click action with the exact frame size, as your tableview and add it to the view as subview?
If another button is clicked, remove this view again and add the new view?
So in case you got this in one single ViewController, which I guess:
Simply change the following for your buttons:
So, in case you got the following 3 buttons
#property (weak, nonatomic) IBOutlet UIButton *buttonA;
#property (weak, nonatomic) IBOutlet UIButton *buttonB;
#property (weak, nonatomic) IBOutlet UIButton *buttonC;
Implement the function, which reacts on these three button events:
- (IBAction)myButtonAction:(UIButton *)sender
{
UIViewController *viewControllerToDisplay;
// Load UIViewController for its view from your .xib-file:
// Alternatively you could do this with switch.case and checking a button's tag-values, but I think is easier to read
if (sender == self.buttonA) viewControllerToDisplay = [ViewControllerA new];
else if (sender == self.buttonB) viewControllerToDisplay = [ViewControllerB new];
else if (sender == self.buttonC) viewControllerToDisplay = [ViewControllerC new];
// Only display the view, if the viewController was created
if (viewControllerToDisplay)
{
viewControllerToDisplay.view.frame = self.tableView.frame;
[self.view addSubview:viewControllerToDisplay];
}
}

Add, remove subview and communicate between subview and main uiviewcontroller

I have created two uiviewcontrollers in storyboard. I am adding second UIview as a subview to 1st view when a button is pressed.
Now, my subview has a done and cancel button, which upon been touched, the subview has to be removed from the main view and needs to send some data back to main view. Is using delegates the only way to solve this? Please explain if there is any other simpler or better option.
Thank you :)
It sounds like the question is just about subviews of the 1st view controller's view. In that case, the 1st view controller can directly inspect all of them. i.e. Say the data that you'd like "passed" between views is the text of a UITextField contained on the subview.
You have an outlet to the subview, probably painted in IB?
// MyViewController.m
#property(weak, nonatomic) IBOutlet UIView *subview; // self.view is it's parent
Create an outlet that connects to whatever subviews you want data from:
#property(weak, nonatomic) IBOutlet UITextField *textField; // probably, subview is it's parent
Hide and show the "dialog":
self.subview.alpha = 0.0; // to hide (alpha is better than 'hidden' because it's animatable
self.subview.alpha = 1.0; // to show
When the button is pressed:
- (IBAction)pressedDoneButton:(id)sender {
self.subview.alpha = 0.0;
// or, prettier:
[UIView animateWithDuration:0.3 animations:^{ self.subview.alpha = 0.0; }];
// the text field still exists, it's just invisible because it's parent is invisible
NSLog(#"user pressed done and the text that she entered is %#", self.textField.text);
}
The point is that data is not being passed between views. The view controller has pointers to views. Some, like buttons generate events for the view controller to react to. Others carry data that the view controller can see.

TextField will NOT resign first responder with UIModalPresentationFormSheet view

I've created a button on one viewController that loads another view modally using the UIModalPresentationFormSheet presentation style. On this loaded view, I have two textFields, and I'm forcing the first textField to be first responder so that the keyboard will appear immediately with the new view. I've set up the textFields to have an action method that is hooked up to "Did End on Exit" event. However, whenever I hit "return" on the keyboard for either textField, the keyboard fails to go away (Here is my code):
// addCustomPage method that is called when button from original view is touched
- (IBAction) addCustomPage:(id)sender
{
NSLog(#"Adding Custom Page");
if (!self.customPageViewController)
{
self.customPageViewController =
[[CustomPageViewController alloc] initWithNibName:#"CustomPageViewController" bundle: nil];
}
customPageViewController.modalPresentationStyle = UIModalPresentationFormSheet;
[self presentModalViewController:customPageViewController animated:YES];
// force keyboard to appear with loaded page on the first textField
[customPageViewController.firstTextField becomeFirstResponder];
}
#interface CustomPageViewController : UIViewController
#property (strong, nonatomic) IBOutlet UITextField *firstTextField;
#property (strong, nonatomic) IBOutlet UITextField *secondTextField;
- (IBAction)keyboardEndOnExit:(id)sender; // DID END ON EXIT EVENT
#end
//in CustomPageViewController.m
-(IBAction)keyboardEndOnExit:(id)sender
{
[sender resignFirstResponder];
}
This is a fairly straight forward problem, and I have no problem normally dismissing keyboards using this technique with basic views and textFields. I'm not sure if using a view in this presentation format, or set up makes things different. Thanks!
You have confirmed that you keyboardEndOnExit method is actually being called?
You could also take a more direct approach by calling [yourTextView resignFirstResponder] when a specific action is take by the user, such as a key pressed etc. I would still check if that method is ever being called using breakpoints or a log.
Have a look at this question. Pretty sure it is the same problem caused by UIModalPresentationFormSheet.

Swapping views - NSWindowController and NSViewController(s)

I'm very new in Mac OS programming. At the moment I'm trying to create simple measurement application which will have one window with the toolbar at the top and the appropriate view in the bottom. Clicking button in the toolbar should result in switching view below it - e.g. clicking on the "Connection" button will show with connection settings, "Measurements" will show current data from the device.
The problem is - I don't know how to handle swapping views, maybe in other words - something I know but not exactly...
I found similar discussion here: NSViewController and multiple subviews from a Nib but there is no answer how to create NSWindowController and how to assign it to the Main window. Because I guess it is necessary to create NSWindowController to be able to swapping views. If I'm wrong, please correct me.
So I'm creating new project (called Sample here) and there is SampleAppDelegate.h file, which looks like:
#interface SampleAppDelegate : NSObject <NSApplicationDelegate> {
#private
NSWindow *window;
}
#property (assign) IBOutlet NSWindow *window;
#end
There is window ivar, which holds the only one window, created from the MainMenu.xib (as I think).
So how should I create NSWindowController for the window from the SampleAppDelegate?
Should I just create my WindowController subclass and in the function
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
of the SampleAppDelegate like this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
MyWindowController *wc = [[MyWindowController alloc] initWithWindow:self.window];
[wc showWindow:self];
self.myWindowController = wc;
[wc release];
}
I'll be very grateful for any hints and help.
Marcin
You shouldn't need an NSWindowController to do view swapping, NSWindowController used (I think) just when you need multiple toplevel windows.
You can just subclass NSViewController for each type of view that you want, put each view into a nib, and call -(NSView *)view when you need a view to put into the bottom part of the window. You should be able to just add it to the window like normal, or put it in an NSBox by using setContentView:view
For your two views you'd create MeasurmentsViewController and a ConnectionViewController. Then you'd create your views in MeasurementsView.nib and ConnectionView.nib, and use those nibs to initialise your view controllers.
Then in your main window, if you were to put an NSBox, if you wanted to put the MeasurementsView into it
NSView *measurementsView = [measurementsViewController view];
[boxAtBottomOfWindow setContentView:measurementsView];
and to put the ConnectionView into it
NSView *connectionView = [connectionViewController view];
[boxAtBottomOfWindow setContentView:connectionView];