from xib to storyboard UIButton or no? - objective-c

I created an xib file(which is a gridView) outside of storyboard. If you create a button on the xib file and wanted to move from that button back to the storyboard and passing GridViewItems. How do you do that? Thanks in advance.

I figured it out. First, you set a push segue on the storyboard from the view you called the grid view to the view you want to go. Of course, you have to connect your button in the grid view xib file to IBOutlet property.
Then add this to the tableView
[cellview.button addTarget:self action:#selector(toNextView:) forControlEvents:UIControlEventTouchUpInside];
and create a function toNextView and perform the segue in the function:
[self.own performSegueWithIdentifier:#"OwnViewToNewView" sender:item];

Related

How can I dynamically fit a xib file view into a storyboard view?

I have setup a simple layout with a button which, when pressed, causes the storyboard subview to load a xib file and assigns its view to the storyboard subview. The problem is that the xib view causes the storyboard subview to 'break' its original constraints:
Here's the main storyboard (left) and xib view (right):
Here's what happens when I press the button on the main storyboard.
- (IBAction)btnShowView:(id)sender {
xibViewController *xib_VC = [[xibViewController alloc] initWithNibName:nil bundle:nil];
xib_VC.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;
//xib_VC.view.frame.size = self.myView.frame.size;
UIView *view = xib_VC.view;
[self.myView addSubview:view];
}
And here's the result - you can see that the xib view has caused the main storyboard subview to go outside of its constraints.
I'm guessing that dynamically changing the storyboard subview overwrites its original view and constraints? Do I have to programatically add these constraints again before assigning the xib view to the storyboard subview?
By your descriptions, it looks like your intention is to put newly created view(xib_VC.view) in the position of myView.
By code you mentioned is adding xib_VC.view to myview as a subview,It doesn't any constraints to it or provides a frame to view.
Setting xib_VC.view.frame.size will not going to work, this is frame for view in xib_VC.
If you just need view, then you don't have to create view controller xib. Just create view xib.
Following line no use, since you are not using a controller.
xib_VC.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;

Losing Segue Action when adding custom image in navigation bar button item

I`m developing a app using Storyboards.
In one ViewController I have a button on navigationBar that links to the second ViewController. This transiction is defined in the storyboard (in this case I have defined a push segue to link the two ViewControllers)
I have changed the image of the button following this post in Stackoverflow.
But the problem is: That change in the View of the button breaks the push segue that I have defined in the storyboard. So the question is: How to still change the background of the BarButton without killing the segue action?
I dont want to programmatically reset the segue using performSegueWithIdentifier. This makes no sense since I already have defined it on the storyboard, so I think that must be another solution.
I think this is going to be your best solution:
In viewDidLoad:
self.navButton = [UIButton buttonWithType:UIButtonTypeCustom];
self.navButton.frame = CGRectMake(0, 0, 30, 30);
[self.navButton setImage:[UIImage imageNamed:#"yourImage"] forState:UIControlStateNormal];
[self.navButton addTarget:self action:#selector(yourNavButtonAction) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *logOutBarButton = [[UIBarButtonItem alloc] initWithCustomView:self.navButton];
[self.navigationItem setRightBarButtonItems:[NSArray arrayWithObject:navButton, nil]];
And in the method handling your navButton tap (yourNavButtonAction from above)
[self performSegueWithIdentifier:#"yourSegueIdentifier" sender:self];
**Note that this will require you to create a storyboard segue that originates from your ViewController itself, as opposed to a button on that ViewController. Control drag from your ViewController to the target ViewController, give the resulting segue an identifier (yourSegueIdentifier above) and you're set.
You cannot use the quoted code and use the segue at the same time. The code introduced a new object, a UIButton that gets the click, so your storyboard object will not get it any more.
You could try adding a standard custom UIButton in storyboard and change the code as follows:
// instead of
UIButton *someButton = [[UIButton alloc] initWithFrame:frameimg];
// use
_someButton.frame = frameimg;
Assuming that your _someButton is the name for the IBOutlet of the button from storyboard.
If this does not work, you should go with performSegueWithIdentifier. I do not see the problem with this, either. You anyway include a #selector with the custom UIButton. Just use it to initiate the segue. You can still configure the segue, etc., in the storyboard, so nothing is lost, right? In fact, the refactoring above seems like more work.

Detecting UIButton touches on a subview

I have added a subview which contains a UIButton control. This subview is added to its superview programatically. I need to perform an action when this button is tapped, but since it's contained within its own subview, I can't hook an IBAction up to the view controller in order to push another view controller.
Is there an easy way to detect that the button is tapped and call a method within its super view?
You can do everything programmatically:
[buttonName addTarget:self action:#selector(methodName:) forControlEvents:UIControlEventTouchUpInside];
and then create your method:
- (void)methodName:(id)sender
{
// Do something.
}
This is all you have to code.

How to TabBarController display differents Items?

On my firstViewController I have a tabbar that contains my firstViewController and a helpViewController.
When I click on a button from the FirstViewController, I push a NewViewController. But, when this view is pushed, I want to change the content from the TabBarController to display other ViewControllers, like infoViewController, optionViewController and NewViewController. Is that possible?
The First Image represents my application. The FirstViewController has a button that will push the NewViewController. When the user clicks this button, I want that my app shows what is in the second image. Is possible?
Yes, this is possible (I just did a proof of concept in Xcode). Assuming that you are using storyboarding, you need to make your initial view controller a UINavigationController otherwise you won't be able to use the push segues. Then, make the first UITabBarViewController the root view controller of the navigation controller. Put an entirely new UITabBarController into the storyboard, and then put a UIButton into the firstViewController and link it via a push segue to the new (second) UITabBarController.
When you tap the button the old tab bar will slide off, and the new one will slide on.
Here's an example of how it all looks:
!!This app uses navigationController and TabBarController!!
Using the storyboard I saw each piece of the app, then I had the Idea: Insted of pushing the NewViewController, how about push a tabBarController? When the user clicks the button, the app will push the tabBarController with 2 TabController`s.
Just add New File to your project, sub classed UITabBarController. Then add this code to the init method of your tabBarController: self.hidesBottonBarWhenPushed = YES;
On ViewDidLoad just alloc and init what views you want to display on the tabBar and
self setViewControllers:[NSArray arrayWithObjects: vc1, vc2, vc3, nil]];
Working fine here :D
You can nest TabBarControllers. But that would look strange. And the first TabBar wouldn't be changed. Pushing a TabBarController into a TabBarController is not possible because TabBarController does not support pushViewController. Thats only possible with a NavigationController.
Anyhow you can change the content of the TabBar completely programatically.

How to wire a Button in a UITableViewCell with its action method

How can I connect a Button in a UITableViewCell with its action method in the UITableViewCell in xcode.
Crtl-Draging from the button to the source doesn´t work.
Create a subclass of UITableViewCell. Then you can hook up your button to an action in your sublass. Connect your button to your own subclass instead of the file's owner.
When dealing with custom cells, note that your "source" is now not the "Files Owner" but rather the "UITableViewCell" object. Hope this does the trick, if not - something is wired wrong in your code / XIB
Do it programmatically. It is the simplest believe me. Every tableViewCell has got a contentView. So all you do is for your specific row in section in -tableView:cellForRowAtIndexPath:
UIButton* cellButton= [UIButton buttonWithType:UIButtonTypeRoundedRect]; // Cane be custom as well
[cellButton addTarget:addTarget:self action:#selector(handleCellButton:) forControlEvents:UIControlEventTouchDown];
// Further changes such as text color whatever ...
[cell.contentView addSubView:cellButton];
//add a handler method for the button
-(void)handleCellButton:(id)sender{}