How to add view with buttons (target-action) to a scroll view - ios

I have created a custom UIViewController with its view in a .xib file created in Interface Builder. The view looks like this:
The view has a UIButton with a target-action pair. The action is a method in the view's view controller. I want to add this view to a UIScrollView, so I created a simple custom view controller which just has as UIScrollView in it. I added the first view controller's view as a subview to the scrollview and set the content size properly.
Everything now works fine, except that when I press the button the application crashes and the 'EXC_BAD_ACCESS' warning comes up with error code 2.
How can I solve this?

There is an action in your view controller that you assign to your button ?

Related

Xcode Storyboard: Can't add View to ViewController

I'm using Xcode 10 (with Swift 5 and for iOS 12) and my app is currently set up like this:
UIViewController 1 (login screen) - segue12 to ->
NavigationController that automatically created
UIViewController 2 with a UITableView - segue23 to ->
UIViewController 3 (detailed info about an item in the UITableView with automatically created "back" button)
The NavigationBar only contains a " ".
I now want to add another UIView with a button below the UITableView but Xcode won't let me drag it below the UITableView, only either into the UITableView or directly underneath "First Responder", which puts the View outside into its own window.
If I delete "My Table View", it won't let me drag in a replacement either.
How do I fix this, so I can add the new UIView + UIButton?
NavigationController that automatically created UIViewController 2
That explains everything. When you drag a navigation controller into the storyboard, what you get is a navigation controller and its root view controller, and the root view controller is a UITableViewController.
Let's step back for a moment. A view controller can and must have exactly one main view. It occupies the whole scene. It cannot be resized. It cannot have a sibling view, because it has no superview for a sibling view to be a child of. It can only have children (subviews).
Well, in this case, ViewController2 is a UITableViewController, and MyTableView is ViewController2’s main view. (A table view controller is always configured this way, and that is what you got when you dragged the navigation controller into the storyboard.) That is why you cannot add more views to it, except as subviews, e.g. prototype cell contents. Your button has no place to go except inside the table view.
So, what to do?
If the extra interface you want to add is just a button, the usual solution is to put it into the navigation bar. It becomes part of the table view controller's navigation item.
If you don't want to do that — that is, if you really want the button and the table view to be siblings of one another in the interface — then you need to replace ViewController2 itself with an ordinary view controller. Now its main view will be an ordinary view, and you can drag anything you like into it, including a table view and a button. More likely, instead of a bare table view, you would use a container view, with an embed segue to a table view controller.

Pushing a ViewController Through a UIScrollView

I have a custom uiscrollview class that has certain views and buttons. When I press a button in this custom scrollview, I want to present a new viewcontroller. Since this scroll view is inside of a view controller, and the custom class doesn't have the authority to push view controllers like a navigation controller, how do I push a new view controller through this uiscrollview class without throwing an error? Whenever I create a new navigationcontroller and an instance of the viewcontroller that I want inside of the method for when the button is pressed, and I present that viewcontroller, I get an error. Also, all of this is programatic and dynamic, not though storyboard. Thanks.
The scrollView must be inside a viewController.
The best way to accomplish your scenario is to put the viewController that contains the scroller inside a navigationController, Then you can push to wharever you want.
Hope this may help
You can try to instantiate viewController through the storyboard, or directly from class, after that present it as a modal view controller from your source UIViewController like this
presentViewController:animated:
This possible leads to animation issues, that could be solved implementing your own animation

Adding toolbar to a ViewController in storyboard

I'm using storyboard to create an iPad app. On the main view I have a toolbar and a bar button item, let's call it "show". Then I have dragged a table view controller into the storyboard. I have also added a subclass of UITableViewController to the files and made the class of the dragged table view controller to be that subclass. And I made a popover segue from the "show" button to the table view controller. It works fine, meaning that when "show" pressed I see the popover showing the correct data that I set in the table view. What I cannot seem to figure out is how to put a toolbar on top of the table view in the popover. I took a step back and used a UIViewController instead of UITableViewController and still cannot add a toolbar by dragging it to the view. Any help will be appreciated.
I ended up putting the TableViewController within a NavigationController and the latter in a PopoverController, all in the code, without using IB. I found this an easier solution to get the toolbar than anything else that might work.

How can i put a small subview on a view xcode

i have a button in my view and i want that when user pushed it a small view should apper on it. there is two image about what i want.
on this view-->
this view
How can i do tahat
Set up the small view as a top-level object in IB (that is, outside any view controller's view) and connect an outlet to refer to it, or set it up programmatically.
Then, in the action method for your button press, add that view as a subview of your view controller's view.
See the View Programming Guide for more details.
Steps:
1. Create a custome view "this view".
2. On click of a button call
- [self.view addSubview:thisview] if just to show the view over the last view.
- [self.view presentViewController:thisview animated:Yes]
3. To remove the view:
[self.view removeFromParentViewController]

Segue from a scrollview

I have a UIscrollview that takes up about half of a screen. The scrollview contains a series of view controllers that have buttons that have segues to another view controller. When that segue is followed, it loads the view controller on top of the current scrollview.
I want that new view controller to act like any other modal segue would act if the button was not within a subview or scrollview. In other words, take up the whole screen.
Can you use segues from within a subview or a scrollview?
Thanks!
The scrollview contains a series of view controllers that have buttons
that have segues to another view controller.
An instance of UIScrollView can't "contain" view controllers -- it can only contain other views. It might contain views that are managed by other view controllers, but if you're using many view controllers all at the same time you may want to read Am I abusing UIViewController Subclassing?.
I ended up just using delegates and protocols between the views in the scrollview and the parent view controller and then I launch the new view from the parent view controller. Seems to be doing the trick.

Resources