Hiding and showing different scrollviews - xcode

So I have two scroll views that I want my XIB to show.
My first scroll view is loaded with the XIB and I want the second to be loaded when a button is pressed.
Does any body know how I can do this?

UIScrollView inherits from UIView. All you have to do is set it's hidden property to hidden. It is a simple set method. By default it will display as long as you add the view.
On your click action or method triggered simply set the property for the UIScrollView like:
[myScrollView setHidden:NO];
And if you want it to disappear again, on a different action:
[myScrollView setHidden:YES];

Related

How to show different views in same window in cocoa?

Is it possible to do navigation within the same window in a mac application ?(Like it is possible in ios apps).I want to show each view in the same window instead of opening different windows on a button click.
e.g When a user clicks a button then the next page should be loaded in the same window.(The next page will have nothing in common with the current page.)
You may use Tab View for easy switching between views on a same window.
UPDATE:
You may also customize your tab view , make it tabless (In the attributes inspector set style to tabless) and use your buttons to switch between views.
You may take help from the following link : http://devcry.heiho.net/2012/01/nstabview-tutorial.html
OR
You may add or remove subviews from your window on button clicks, using
[[yourWindow contentView] addSubview: yourSubview]; // Add subview to window
[yourSubview removeFromSuperview]; //Remove subview
UPDATE:
Steps to swap between views using a tabless tab view.
Drag a NSTabView to your xib.
Set the no. of tabs in attribute inspector to no. of views you want.
Design each view of the tab as per your requirement.
Now in the attribute inspector of tabview, set style to tabless.
Now drag the buttons you want to use for swapping between views. Suppose Button0 and Button1 are for 1st and 2nd view of your tab view.
Create a IBOutlet for your NSTabView in your .h file. Bind it to the referencing outlet of you tabview.
IBOutLet NSTabView* tabview;
Set a IBAction for both your buttons in your .h class file.
In the button action method for button1, use
- (IBAction)button1clicked:(id)sender
{
[tab selectTabViewItemAtIndex:0];
}
Similarly in button2 action method use:
[tab selectTabViewItemAtIndex:1];
In this way you can have any no. of views and you may select any view on button click using
[tab selectTabViewItemAtIndex:(index of the view you want to load)];
In general you want to google for view swapping.
There are tons of examples out there. Some from Apple and lots elsewhere.
Much of it is very similar to iOS.
You need to read the docs a bit too.
Understand NSView and how to load views from nibs, how to create view objects in code, how to add a subview and how to remove a view.
There are many approaches to having different views for different reasons. The right approach is a combination of style, experience and what your app actually needs to do.
Cocoa includes NSBox, NSTabView, and lots of others. Those two can be configured to not display any visual indication that they are containers.
You will also need to understand at least a little about NSWindow to understand its content view (the root container of other views generally)

Configuring the backBarButtonItem of a View Controller's Navigation Item in a Storyboard

It's easy enough to drag and drop bar button items onto a view controller's navigation bar in a storyboard in Interface Builder. In this way, you can set the leftBarButtonItem and rightBarButtonItem outlets of the view controller's navigation item. But there's also a backBarButtonItem outlet, and it's not obvious at all how to set it. How can I set a custom back bar button item using Interface Builder?
Select the view controller whose navigation items you want to change. The black bar displaying the identity of the view controller changes to an iconified tray of its referenced objects.
Drag and drop a bar button item from the object library onto the tray.
Right-click on the view controller's navigation item in the main object tray on the left-hand side. Wire up the newly added button as the navigation item's backBarButtonItem outlet.
Select the bar button and configure it in any way you choose with the Attributes Inspector.
As #wcochran noted above, when working with viewControllers pushed onto a navigationController's stack, the backBarButtonItem outlet is already wired and can't be changed. Furthermore, selecting the child VC's navigationItem and changing the Back Button text in IB doesn't do what you would expect.
Now you might think that replacing the child VC's backBarButtonItem would solve the problem, but it doesn't. Confusingly, if you want to set the title of the back button of a child VC, you have to set the back button title of its parent (!), like so:
- (void)viewWillAppear:(BOOL)animated // in the parent VC!
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
}
This won't do anything on the parent VC. In fact, if the parent is the navigationController's RootViewController, there won't be a back button at all. But the child will inherit (or pick up) the back button you've created.
This only applies to the immediate child VC, so if you want to maintain the label down through the navigationController's stack you need to set it on each parent.
Thanks to #wiliz in #iphonedev for explaining this to me.
As #AdamBlock noted above, you have to set things right in the parent VC.
He shows how to do this programmatically. It is also possible to do this in interface builder.
Select the parent VC
Select the navigation Item
Open the Attributes inspector
Set the title for the Back Button.
In Interface Builder, you can change the Navigation Item back button's title.
Programmatically, you can set a custom back button in your view controller's viewDidLoad method. In this example we set the button's image to an image named "customImage.png":
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:#"Custom" style:UIBarButtonItemStyleBordered target:self action:nil];
// Set custom image here
backButton.image = [UIImage imageNamed:#"customImage.png"];
self.navigationItem.backBarButtonItem = backButton;
}

how do i stop NSTextField blocking right clicks for NSTableView rows?

As the title suggest, I've enabled right clicks for my tableview with customized rows. Anywhere there is an NSTextField, it blocks the right click.
is there a userInteractionEnabled equivalent for cocoa like on the iphone?
I though I probably needed to subclass everything in my NSTableCellView subclass, but I just needed to override -(NSView*)hitTest:(NSPoint)aPoint method in my NSTableCellView subclass to return self.
I'm able to right click on text fields within my custom view based table view cell. Here is how I configure it:
NSTextField *tf = [[NSTextField alloc] initWithFrame:NSZeroRect];
self.textField = tf;
self.imageView.autoresizingMask=NSViewWidthSizable;
self.textField.editable=NO;
self.textField.selectable=NO;
self.textField.drawsBackground=NO;
self.textField.bordered=NO;
self.textField.bezeled=NO;
self.textField.target=self;
self.textField.action=#selector(textDidEndEditing:);
[self.textField.cell setLineBreakMode:NSLineBreakByTruncatingMiddle];
Also, make sure you are setting the -menu property of NSTableView and not the cell view to enable to menu. (I don't know if that will make a difference to your issue but it is how I do right clicking in a table view.)

Cocoa: hide custom scroller of textview on application startup

I've created a custom scroller for my textview (initiating it in the awakeFromNib method of the scrollview) and now I want to let the user chose if he wants to show the scrollbar on application startup. The problem is that the scroller always appears even if I hide it immediately after I created it and set it to be the scroller of the scrollview. The weird thing is that trying to hide the scroller after an event has been triggered (for example by clicking on a checkbox in the preferences) the scroller properly hides and shows. What I'm I doing wrong? Any help is appreciated!
The weird thing is that before adding the custom scroller to the scrollview I have to use setHasVerticalScroller:YES, otherwise I can't scroll using the two-fingers scroll gesture. Then, if the user doesn't want the scrollbar to be shown I have to use setHasVerticalScroller:NO in the document's windowControllerDidLoadNib method, using it just after having added the scrollbar in the scrollview's awakeFromNib method won't work. Well, at least now it seems to work!
I've always used IB to set up scrollbars and then used the following line if I want to suppress one of them:
[self.aScrollView setHasHorizontalScroller:NO]; // so only the vertical scrollbar is active
Try using that line in awakeFromNib, later setting it to YES if user chooses, rather than using the "hidden" property.
P.S. An NSTextView added in IB is always embedded in an NSScrollView, and it's the scrollview that governs the scrollbars. So if the above doesn't work, try calling setHasWhateverScroller on the superview of your textview:
[[[aTextView superview] superview] setHasHorizontalScroller:NO];
If you get an "unrecognized selector" error, then try explicitly casting the superview to NSScrollView (which will work only if the superview really is an instance of NSScrollView):
[(NSScrollView *)[[aTextView superview] superview] setHasHorizontalScroller:NO];

How do I force an NSPopUpButtonCell to redisplay itself?

I'm creating a custom subclass of NSPopUpButton and NSPopUpButtonCell to display a lone icon on top of the button instead of the usual text.
To do this I'm overriding
- (void)drawInteriorWithFrame:(NSRect)cellFrame inView:(NSView *)controlView
in my button cell subclass to draw my icon at the appropriate size and location. When the user mouses over the button I then want to change the image. My only problem is I can't figure out how to make the button cell redisplay (by calling drawInterior... again). By default it only seems to redisplay when the button is clicked or when focus is moved to another window. I've tried setNeedsDisplay:YES but that doesn't seem to do it.
The way I found to do this was to insert a call to
[self updateCell:self.cell];
inside my NSPopUpButton subclass immediately after any point I changed the image displayed by the cell.

Resources