Segmented controls affect button - ios

I need to set my segmented control to affect where my button sends a user. Yes sends them to a new screen, No sends them to the next screen.
1st Segment:
UISegmentedControl *par;
Yes (0) sends users to the UIViewController ParError on click of the next button.
No (1 - Default) sends users to the UIViewController TermsUI on click on the next button.
2nd Segment:
UISegmentedControl *basicOp;
Yes (0) sends users to the UIViewController Unsupported on click of the next button.
No (1 - Default) sends users to the UIViewController TermsUI on click of the next button.
I've tried If statement and It got to complicated, and I kept getting errors when both are selected. Anyone want a job? I can't figure it out with any of the developer guides. Help please?
I'm new to Xcode and Objective-C so please explain steps of how to do things.
NOTE: I don't need this to be as subviews. What it is doing is pushing the user to another screen witch gives new instructions on what to do, and then takes them to another screen different from the original.

For getting the current value selected of your SegmentControls use the property .selectedSegmentIndex. That will return the index of the option selected in your segmentControl (More information about UISegmentedControl class reference),
As I understand from your question, par segment has only two elements: YES, NO. On that specific order so,
if (par.selectedSegmentIndex == 0) {
// YES is selected, perform the action for going to ParError UIViewController
} else {
// NO is selected, perform the action for going to TermsUI UIViewController
}
And in the case that basicOp has also two options on the same order : YES, NO
if (basicOp.selectedSegmentIndex == 0) {
// YES is selected, perform the action for going to Unsupported UIViewController
} else {
// NO is selected, perform the action for going to TermsUI UIViewController
}
Assuming you want to achieve the following :
If par is on YES (No matter what value has basicOp), the app sends user to ParError UIViewController
If par is on NO and basicOp is on YES, the app sends user to Unsupported UIViewController
If par is on NO and basicOp is on , the app sends user to TermsUI UIViewController
The conditionals you should have would be :
if (par.selectedSegmentIndex == 0) {
// YES is selected, perform the action for going to ParError UIViewController
} else if (basicOp.selectedSegmentIndex == 0) {
// YES is selected, perform the action for going to Unsupported UIViewController
} else if (par.selectedSegmentIndex == 1 || basicOp.selectedSegmentIndex == 1) {
// NO is selected, perform the action for going to TermsUI UIViewController
}

Related

Ios navigation - custom back button or removing viewcontriller from stack?

I'am new to ios. I have several view controllers and I want back button to get user to level up controller. Example.
But if user comes from gameOver view, back button sent him back to gameOver and I don't want such behavior (I want user to be sent at second level (games level) controller as shown). On android I could set the pop behavior for the navigation actions with mouse very easily.
What is the correct way to do the same in ios? Or I have to create the custom back button and do everything manually?
Using Swift this can be achieved using below code:
func popToViewController(_ specificViewController: Swift.AnyClass) {
let viewControllers = self.navigationController!.viewControllers
for eachViewController in viewControllers {
if eachViewController.isKind(of: specificViewController) {
self.navigationController!.popToViewController(eachViewController, animated: true)
break;
}
}
}
Usage:
self.popToViewController(gameLevelViewController.self)

Setting badge on a tabbar item of chosen type (without knowing its index)

I have a tabBar in my app. I want to display a badge with a number of unread messages when notification comes. So far my code is below.
The problem is that the messages tab isn't always the first item in the list (the order varies for different app settings, but it's always there). How do I set the badge on it if I don't know which index it has?
if let item = self.tabBar.items?.first {
var count = messages.count
if item.badgeValue != nil {
count += Int(item.badgeValue!) ?? 0
}
item.badgeValue = "\(count)"
}
You are getting first item in first line - so it's working as expected.
You need to organize your UITabBar this way, that you can always identify on which index messages are presented.
One idea would be to keep a reference to it when you configure your UITabBar - this way that you can always find under which index messages are shown.
Best way would be to keep corresponding array of views you keep behind your UITabBar, and then find the one you need.
If you are using UITabBarController you will get it for free - all UIViewController are accessible directly via property named viewControllers.
If you have custom ViewController and just UITabBar - you just need to build similar logic that will allow you to keep track on which index certain view controller is shown.
Assuming your tabbar is managed by a UITabBarController, you can determine the index by checking the type of the view controllers in your tab bar controller.
class MessagesViewController: UIViewController {
// ... your messages vc
}
if let index = tabbarController.viewControllers?.firstIndex(where: { $0 is MessagesViewController }) {
tabbarController.tabBar.items?[index].badgeValue = "..."
}

iOS: Good way to pass value from image button to func in the class

Initially I had two buttons with titles: "+" and "-". Both buttons are located in table cell. I have used protocol and delegate to pass value from button title to function call which depends on input params. If input parameter is "-" - decrease value, if "+" - increase.
But later I removed titles and replaced buttons with respective images. And here I faced an issue - I cannot call function properly because title is blank.
I have implemented the following workaround. I have set Accessibility Identifier for both buttons. For example for +:
And in cell class I used accessibilityIdentifier:
#IBAction func didPressOrderCellButton(_ sender: UIButton) {
cellDelegate?.didPressOrderCellButton(order: order!, menuItem: menuItem!, action: sender.accessibilityIdentifier!)
}
But... I have some doubts if it's proper way to do this. Will it create any issues in future if I decide to work with accessibility feature?
I do not want to use title and add code to ViewController class to hide it every time view is loaded or appeared. I want to avoid such solutions in my code because it's too difficult to support such solutions I believe.
I have tried to find another button identifier that I can use, but didn't succeed.
The simple approach would be just set the tag value inside your button and then check the same tag value and perform your operation Plus or Minus accordingly.
For instance:-
if sender.tag == 0 {
//Do minus
} else if sender.tag == 1 {
//Do plus
}

iOS - How to use a small view in different view controllers in Swift

I have a progress bar (with its own controller). This bar is supposed to be shown in different views depending on which view is visible. As the progress will be same, If possible I don't want to create many progress bar in many views rather I want to use same instance in all these views. Also in that way when I need to change any property of the progress bar it will be reflected commonly, which is required.
Please suggest me how can I use this common view. And also if my strategy is wrong, what would be the better design for such scenarios.
1) Well you have 2 options. You can declare a new Class ViewBox (or whatever name) and then use that inside your code
First View Controller
var box:ViewBox = ViewBox()
When you segue or transition to your next screen, you can have a predefined variable var box:ViewBox!. Then say when you press a button, the button has a function called transition.
//Now setup the transition inside the Storyboard and name the identifier "toThirdViewController"
override func prepareForSegue(segue:UIStoryboardSegue, sender:AnyObject?) {
if(segue.identifier == "toThirdViewController") {
var vc = segue.destinationViewController as! `nextViewController` //The class of your next viewcontroller goes here
vc.box = self.box
}
//Since The SecondViewController doesn't need ViewBox, we don't need it there.
}
where
nextViewController:UIViewController {
var box:ViewBox!
}
Or you could do a much simpler way and that is to look up a UIPageViewController :)

Pass the selected segment in UISegmentControl to a DetailViewController for Editing

I seem to be missing something very simple here.
I have a UIViewController which contains a UISegmentControl with two segments ("shown" & "not shown").
The user selects one in this view controller and fills in some information into text fields which all gets saved to a table view controller.
When I click on a cell to edit the information, I can't get the selected segment to show, so if I select "Not shown" in this cell when saving, I want it to show "Not Shown" selected when I edit the cell.
I then of course want to provide the user the ability to change from "Not Shown" to "Shown" with the UISegmentControl.
My code for saving the UISegment Control in the save method of the creating View Controller is:
contract.wasShown = #(self.isShownSegment.selectedSegmentIndex == 0);
I'm using Core Data here.
So in the detailViewController, I have tried a few things but with no luck (it's always showing the first segment).
if ([contract.wasShown boolValue]) {
contract.wasShown = #(self.isShownSegment.selectedSegmentIndex == 0);
}
else {
contract.wasShown = #(self.isShownSegment.selectedSegmentIndex == 1);
}
What do I need to do to get the selected segment shown and then what should I put in the save method of the Detail View to change that selection if possible?
Thanks!
Sorry all - this was just me being stupid.
Implemented with the following code in viewDidLoad:
if ([contract.wasShown boolValue])
{
self.isShownSegment.selectedSegmentIndex = 0;
}
else
{
self.isShownSegment.selectedSegmentIndex = 1;
}

Resources