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;
}
Related
I'm building an app which which has built in with 2 different tabs. First tab is is "Home" which basically has a tableview with cells that configured from an api.(The api gets me country names for now)
Those cells also have a "Star" button which prints the data of the specific cell for now.
Second tab is "Saved" tab(SavedViewController), where I want to show the "starred" countries, using a tableview.
You can see the image below in order to get an idea for the app.
App simulation Image
The star button has a function in my CountriesTableViewCell. I'm using a saveButtonDelegate in order to let the SavedViewController know about an item is going to be saved. The code in CountriesTableViewCell for star button is as below.
#objc func buttonTapped() {
//If Button is selected fill the image. Else unfill it.
if !isSaveButtonSelected {
saveButton.setImage(UIImage(systemName: "star.fill"), for: .normal)
isSaveButtonSelected = true
saveButtonDelegate?.saveButtonClicked(with: countryData) //Checking if save button is clicked
}
}
countryData is the data that I get from the api, and this is the data I want to pass to SavedViewController.
struct CountryData: Codable {
let name : String
}
So on the SavedViewController, I'm handling the data using the SaveButtonProtocol conformance as below:
extension SavedViewController: SaveButtonProtocol {
func saveButtonClicked(with data: CountryData) {
countryDataArray.append(data)
print("saveButtonClicked")
print("countryData in savevc is \(countryDataArray)")
DispatchQueue.main.async {
self.countriesTableView.reloadData()
}
}
}
Whenever I click the star button on the first tab, this function is getting called on SavedViewController. So whenever I click to button, those print statements above work fine.
The problem is, whenever the star button is clicked, it should append the data of the current clicked cell to countryDataArray in SavedViewController. But the array is not populating as it should.
Let's say I pressed the first cell's star button, my print("countryData in savevc is (countryDataArray)") statement prints : ["Vatican City"], then I press the second cell's star button it only prints ["Ethiopia"] while it should print ["Vatican City", "Ethiopia"]
Why this issue is happening? My best guess is I'm delegating SavedViewController from the cell class so it behaves differently for every other cell. If that is the problem what should I do to solve it?
Many Thanks.
You should store your data in a shared (static array) object so you only have one source and add the saved indicator in your country struct so you do not rely on what is displayed in one view controller.
I know this is a frequently asked question throughout the forums and I hate to ask another seemingly simplistic question, but I can't manage to find a solution on data passing in regards to my specific circumstance.
Basically, I have a view controller embedded within a navigation controller; of which displays a segmented bar, acting as a 'profile selector'. After selection, I want a series of images on another view to be changed after different profiles are selected, but the data passing isn't seem to be working whatsoever. I'm unsure if delegate is required within my specific circumstance.
Essentially; I'd just love an example of how to pass a case value for a segmented bar so I would be able to perform a simple case statement like follows: (where the case values have been passed from the previous view controller)
#IBAction func chooseImage(sender: AnyObject) {
switch myPhotoSegment.selectedSegmentIndex {
//if first segment selected
case 0:
//stop image animation if currently animating
newImageView.stopAnimating()
//update displayed image
newImageView.image = UIImage(named: "1.jpg")
//if second segment selected
case 1:
//stop image animation if currently animating
newImageView.stopAnimating()
//update displayed image
newImageView.image = UIImage(named: "2.jpg")
//if third segment selected
case 2:
//stop image animation if currently animating
newImageView.stopAnimating()
//update displayed image
newImageView.image = UIImage(named: "3.jpg")
//if fourth segment selected
case 3:
//stop image animation if currently animating
newImageView.stopAnimating()
//update displayed image
newImageView.image = UIImage(named: "4.jpg")
//by default, no segment selected
default:
newImageView.image = nil
}
}
I know my problem is long and probably poorly explained, but any feedback would be greatly appreciated. I kind of struggle with the whole logic and understanding of passing data, so if you could break down the solution for me as simply as possible; that would be incredible.
To share data between those two classes they either need to know about each other or know about a shared object. For example, you could create a singleton data model that contains the properties you want to pass back and forth.
private let instance = MySingleton()
class MySingleton {
var somethingImInterestedIn: String?
var sharedInstance: MySingleton {
get {
return instance
}
}
}
Each class would get a reference to this by using:
MySingletion.sharedInstance
Once they have the reference to the singleton they can set or get any of the properties in that object. For your case you would want to store an enum instead of a String.
I have a table view in my profileViewController that display user's data such : first name, last name , birthday , gender...
I want to edit this table view by clicking in my custom edit button and i can after edit it by mouse and keyboard in ever selected row.
I have implemented a method :
-(IBAction)editTableView:(UIButton *)sender {
if ( sender == self.editButton) {
NSLog(#"Editing");
[super setEditing:TRUE];
[self.userTableView setEditing:TRUE];
self.editing = YES;
}
}
I have searched on the net but I find that edit table view means add or delete a row in this table view and my purpose is to can edit ever row selected by keyboard and mouse ...
A typical interface is this: The user taps on a cell, and you present a presented view controller where the user can edit the same data that appears in that cell. For example, if the cell contains a name label, the presented view controller contains a name text field. If the cell contains a gender label, the presented view controller contains a gender segmented control. The user edits, and then taps Cancel or Done. You dismiss the presented view controller. If the user tapped Done, you also save the user's changes into your data model and reload the data into the table.
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
}
We have a iPad app which includes a two-column news reader. The left view contains the list of news of which some link directly to a news and some push another view controller with another list of news. This will also cause a UIButton to be set as the leftBarButtonItem of the navigation bar. If we are on first level, a simple image that cannot be tapped will be the leftBarButtonItem.
My goal is now to have a test that taps every news on the first level. If a news leads to a second level list, it should tap the UIButton in the navigation bar.
How can I check, if the leftBarButtonItem is "tappable"? Since it can be either an image or a button, just calling navigationBar().leftButton().tap() will lead to an error if it's an image.
I'm also using the tuneup library if that's any help.
Almost all elements in UIAutomation could be tapped. It does not matter if it is an Image, View or a Button. You will get an error in case an object you are trying to tap is invalid.
How to check:
if ( navigationBar().leftButton().checkIsValid() )
{
navigationBar().leftButton().tap();
}
else
{
//do what you need.
}
or you can check if an object you are trying to tap is a button, for example (not the best way but it works):
if ( navigationBar().leftButton().toString() == "[object UIAButton]" )
{
navigationBar().leftButton().tap();
}
else
{
//do what you need.
}
checkIsValid() is available for all UI elements. It will return true if an object exists.
toString() will return [object UIAElementNil] if element is invalid or will return [object UIAButton] or [object UIAImage].
Also try to use Apple documentation:
http://developer.apple.com/library/ios/#documentation/ToolsLanguages/Reference/UIAElementClassReference/UIAElement/UIAElement.html
You can simply use
if (navigationBar().leftButton().exists)
{
navigationBar().leftButton().tap();
}
else
{
//do what you need.
}