I'm creating an application with a section that displays blog entries with information gathered from an XML-parser. I've added a transparent button over each entry like this:
UIButton *currentEntryButton = [UIButton buttonWithType:UIButtonTypeCustom];
currentEntryButton.frame = CGRectMake(0, 0, subViewWidth, currentEntryHeight);
I want the button to open up a webpage if there's a URL in the current entry. So what I want to do is make each button open the URL in a webView on click. But because I don't know how many entries there will be in the view I can't define a function to handle the on click event.
My plan was to define the actions when each button is created in the for-loop, but how can I do that?
I can't use
[currentEntryButton addTarget:self
action:#selector(aMethod:)
forControlEvents:UIControlEventTouchDown];
because each button is supposed to commit an individual action.
This is what it looks like with roundRectButton style
Thanks in advance.
aMethod: takes a parameter - the button. Hence you can tell which button was tapped, and you can perform a different action depending on which one it was. You can distinguish the buttons by title, by tag (if you give them different tags), and so on.
Related
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)
I need to add a customised dropdown UIView into the UITable section header as part of the design requirements. I have successfully added the the dropdown UIView into the section header but when I try to click on the Title inside the dropdown, it wouldn't recognise that I have make a selection on my dropdown UIView, instead it will fire off didSelectRowAtIndexPath of the big UITableView underneath. It seems like I can't make a selection of items that are display outside the section header height if it's added to the section header.
I'm not using a UITableView for the popup UIView. Each row is a UIButton and attached with Touch Up Inside event listener. When I try to click on the buttons, it wouldn't detect I clicked on the buttons but it will take it that I've clicked on Cupertino. A tag is attached to each each button.
This is an example of how I handle the event.
-(IBAction)menuButtonPressed:(UIButton *)sender
{
[sender setSelected:!sender.isSelected];
switch (sender.tag)
{
case 0:
#Do something
break;
case 1:
#Do something
break;
}
}
When I try to expand the section header height, the buttons that fall inside the section header can be clicked. For e.g. Title 1 can be clicked but Title 2 and 3 cannot.
If your project does not restrict you to use a third party library, i would suggest using FPPopover. Its customisable and you can make it look like yours if you want to. Plus it has the method buttonClicked which you can use to solve your problem. Also it behaves much like the UIPopover but you can use it on the iPhone as well.
Hope this helps
I can't get the standard back button of iOS into a navigationBar because I can't find it in the Object Library, so can I do it with code or something else?
I just want the normal, standard, blue back button - you know which I mean.
To "automatically" have a back button you need first have a UINavigationController. Then you need to take a different UIViewController and add it as the root view controller in UINavigationController's init method:
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:someOtherViewController];
Be sure to also set a title for someOtherViewController, usually in it's viewDidLoad or initializer. I'll tell you why this is important in a second:
self.title = #"Some other VC";
Then take a second UIViewController and push it onto your navigation controller:
[navigationController pushViewController:anotherViewController animated:YES];
You now have two UIViewControllers on your navigation stack: someOtherViewController and anotherViewController.
Your view will now have a back button with "Some other VC" in it. This is the title of the view controller that was just moved out of view:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UINavigationController_Class/Reference/Reference.html
http://simplecode.me/2011/09/04/an-introduction-to-uinavigationcontroller/
I would also suggest reading up on how UINavigationControllers work and searching this site a bit more for customizing the back button. There are plenty of threads about it.
You can't add the back button yourself. The back button is part of the Navigation controller. If you embed a Navigation controller into your view(s), the back button will appear and be populated by the name of the previous view.
If you're using storyboards select your view controller, then in top menu choose "editor" -> "embed in" -> "navigation controller".
Edit: Here is an exmaple.
I'm running Xcode 7.2. This was driving me crazy, but I figured it out. Here are all the pieces you need to make the Back button appear (make a test project to prove it):
1) You have to have a Navigation Controller and it has to be set to be the initial view controller. So add the Navigation Controller, you will import two tables. Click on the Navigation Controller and on the properties list, check the box that reads "Is Initial View Controller". You will now see and arrow pointing to this view.
2) In our case we want a ViewController and not the included / connected TableViewController, so delete the TableViewController (RootController) and add a new ViewController.
3) Connect the Navigation Controller to the new ViewController by clicking on the top bar of the Navigation controller and orange circle with the arrow pointing left. Hold the Control button on your keyboard down and click and drag from the orange circle to the ViewController and let go. When given the list of options on how to connect the two views, select 'root view controller'.
Done! Now you the functioning navigation bar and you automatically get the back arrow on all segues added. Test this. Add another ViewController and connect to it with a button on the existing ViewController. Use the Control-click-drag approach from the button to the newest ViewController. Select the 'show' option for the new segue you created.
Run it. You'll see the back option has automatically appeared when you click the button and moved to the newest ViewController.
This is all provided by the Navigation Controller, but only when you make another controller the RootController. Happy navigating!
In my UITabbar Application (created using xCode) I have 6 tab buttons.
In the first tab I have 5 UIButttons which should load other 5 corresponding tabs when clicked on the buttons.
First one is the index/home page others are the different modules with a nib file for each of the modules.
My question is when clicked on the Button1 it should load tabbar 1(index's start from 0 for Tabbar App) and when clicked on the Button2 it should load tabbar 2 and so on.
In the IB action I have written the following code
-(IBAction)Button1:(id)sender
{
firstViewController = [[FirstViewController alloc] initWithNibName:#"FirstView"bundle:nil];
[self.view addSubview:firstViewController.view];
}
To the best of my knowledge this paints(adds) the firstViewController on the tabbar 0 instead of calling the tabbar index 1.
Should I try
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;
If so where should I write this?
Create a ParentTabbarController, which will derive from UITabBarController. Now place the subcontroller in this ParentTabBarController. Define a method in ParentTabBarController which will take care of placing the tab view in corresponding tabbar.
From your action on button tap you need to ping the parent controller method with the argument (may be index like 1,2 etc) which will place the tab on the basis of the provided index.
I hope this will help you identify the place to place the above mentioned code.
Seems like this would be a simple thing, but I cannot find any good info on it.
I just want to be able to 'force' the back button to show. I have a main view and when I transfer to another view there is no back button. But then if I transfer to a 3rd view, the back button appears. This seems to always be the case. Only the 2nd transferred to view shows a back button. I need it to show up on all views except the main view.
I dont need to override it, just simply force it to show where it is not showing...
I know this is an old question, but this might help...
...sometimes you need to embed a view inside a Navigation Controller for the back button to show. Simply select your view controller and select Editor -> Embed In -> Navigation Controller.
If the button does not show (usually if you are presenting a modal view rather than push) then you will need to manually make a 'Cancel' button. Create an action for this and in the method put (I call my method switchback):
-(IBAction)switchback:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
Patch's solution (dismissModalViewController)had been unfortunately deprecated and replaced by dismissViewControllerAnimated.
Here is the current one:
-(IBAction)switchback:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}