I am trying to have numbers change by different amounts, by the press of one button. I am new to xcode and do not know how to do this, any help would be nice.
I want the number to change to 15, but only when I press the button for a second time. Then, I would like, upon a third press, for the number to change 30.
press 1: from "0" to "5",
press 2: from "5" to "15",
press 3: from "15" to 30", I want to learn how to add different amounts
-(IBAction)changep1:(id) sender {
p1score.text = #"5";
if (p1score.text = #"5"){
p1score.text = #"15";
//Even if the above worked, I do not know how I would write the code to change it to 30. }
It sounds like you probably want to add a property to your view controller to store Player 1's score, something like this:
#property (nonatomic, assign) NSInteger p1Score;
Then in your init method, you can give this property an initial value:
self.p1Score = 0; // you can set this to any integral value you want
Then, in your button tap method (changep1) you can do something like this:
- (IBAction)changep1:(id)sender
{
// add 5 (or any value you want) to p1Score
self.p1Score = self.p1Score + 5;
// update the display text. in code below %d is replaced with the value of self.p1Score
p1score.text = [NSString stringWithFormat:#"%d", self.p1Score];
}
Related
I am writing an UI test case, in which I need to perform an action, and then on the current page, scroll the only UITableView to the bottom to check if specific text shows up inside the last cell in the UITableView.
Right now the only way I can think of is to scroll it using app.tables.cells.element(boundBy: 0).swipeUp(), but if there are too many cells, it doesn't scroll all the way to the bottom. And the number of cells in the UITableView is not always the same, I cannot swipe up more than once because there might be only one cell in the table.
One way you could go about this is by getting the last cell from the tableView. Then, run a while loop that scrolls and checks to see if the cell isHittable between each scroll. Once it's determined that isHittable == true, the element can then be asserted against.
https://developer.apple.com/documentation/xctest/xcuielement/1500561-ishittable
It would look something like this (Swift answer):
In your XCTestCase file, write a query to identify the table. Then, a subsequent query to identify the last cell.
let tableView = app.descendants(matching: .table).firstMatch
guard let lastCell = tableView.cells.allElementsBoundByIndex.last else { return }
Use a while loop to determine whether or not the cell isHittable/is on screen. Note: isHittable relies on the cell's userInteractionEnabled property being set to true
//Add in a count, so that the loop can escape if it's scrolled too many times
let MAX_SCROLLS = 10
var count = 0
while lastCell.isHittable == false && count < MAX_SCROLLS {
apps.swipeUp()
count += 1
}
Check the cell's text using the label property, and compare it against the expected text.
//If there is only one label within the cell
let textInLastCell = lastCell.descendants(matching: .staticText).firstMatch
XCTAssertTrue(textInLastCell.label == "Expected Text" && textInLastCell.isHittable)
Blaines answer lead me to dig a little bit more into this topic and I found a different solution that worked for me:
func testTheTest() {
let app = XCUIApplication()
app.launch()
// Opens a menu in my app which contains the table view
app.buttons["openMenu"].tap()
// Get a handle for the tableView
let listpagetableviewTable = app.tables["myTableView"]
// Get a handle for the not yet existing cell by its content text
let cell = listpagetableviewTable.staticTexts["This text is from the cell"]
// Swipe down until it is visible
while !cell.exists {
app.swipeUp()
}
// Interact with it when visible
cell.tap()
}
One thing I had to do for this in order to work is set isAccessibilityElement to true and also assign accessibilityLabel as a String to the table view so it can be queried by it within the test code.
This might not be best practice but for what I could see in my test it works very well. I don't know how it would work when the cell has no text, one might be able to reference the cell(which is not really directly referenced here) by an image view or something else. It's obviously missing the counter from Blaines answer but I left it out for simplicity reasons.
I have code with several uitextfields that will be used to input numbers, and I want to add these numbers together to update a uilabel.
I can do all the updating and the labels and fields, but can't get the addition to work.
Just now I have:
label.text = (textfield1.text + textfield2.text);
I assume I need to convert these textfield inputs to an int, but not sure how to do that...
there is a couple of extra steps you have to do:
convert the string value of your text filed into numerical value
do the math there
and convert it back.
For example (i use float in my case, you can change that to whatever type you want):
float textField1Value = [textfield1.text floarValue];
float textField2Value = [textfield2.text floarValue];
label.text = [NSString stringWithFormat:#"%f", textField1Value + textField2Value];
Hope that helps.
I have come across a simple issue that has totally flummoxed me for some reason.
I have one view controller (which I will call the Info page) which has a text view contained within it. The text view holds a large amount of information in the following format;
Header A
Information about Header A
Header B
Information about Header B
(and so on).
Previous to this, there are viewcontrollers for each of the Headers; on each of which there is a button to segue to the Info Page.
My problem is pretty simple. When the user presses the segue button to transfer to the Info page, how do I code a solution where by the text view automatically scrolls to the specific Header information.
For example, say the user is in the view controller for Header D. He or she clicks the button to segue to the Info page. Upon arrival, how do I make the text view scroll automatically to show the information for Header D.
My apologies if I haven't been clear enough with my question. Obviously I have set up the text box, and the segues... just this final set is alluding me even after spending an age looking around for the answer! Even just directing me to some reading on the subject I may have missed would be great.
Thanks for your help.
This might not be the cleanest solution to this, but this should work:
This goes in the first ViewController's buttons. Just iterate through the buttons setting the integer from 1 to 4 depending on which button you press.
-(void)firstBtnTUI
{
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:#"HEADER"];
//...
}
This goes in ViewDidLoad (or similar) in the pushed view controller
int headerNum = [[NSUserDefaults standardUserDefaults] integerForKey:#"HEADER"];
CGFloat offset;
if(headerNum == 1)
{
offset = firstHeader.frame.origin.y;
}else if(headerNum == 2)
{
offset = secondHeader.frame.origin.y;
}else if(headerNum == 3)
{
offset = thirdHeader.frame.origin.y;
}else if(headerNum == 4)
{
offset = fourthHeader.frame.origin.y;
}
[scrollview setContentOffset:offset animated:NO];
now I have 2 search bar at one page
the first problem is How can I make a search button always show although no text at searchbar.text?
the second problem is, I have a Table View that Will show a different list expend which search bar I choose, how can I do it well?
I can set a variable that change everytime the search bar is active. However is there a way to see which search bar is currently the active search bar?
The simplest way to check which view are you working with, is assigning the tag property:
firstSearchBar.tag = 100;
secondSearchBar.tag = 200;
You can easily check it:
if(searhBar.tag == 100) {
// it is first search bar
} else if(searchBar.tag == 200) {
// it is second search bar
}
Now, the second part. If you want to show cancel button, you can do it in this way:
searchBar.showsCancelButton = YES;
If you want to show scope bar:
searchBar.showsScopeBar = YES;
If you want to show search results button:
searchBar.showsSearchResultsButton = YES;
EDIT: If you wish to show Search keyboard button even if there's no text entered, you can do it in this way:
UITextField *searchField = (UItextField *)[[searchBar subviews] objectAtIndex:0];
[searchField.enablesReturnKeyAutomatically = NO;
I recommend you reading UISearchBar's documentation.
ok, here is the story...
I have 3 textfield for user to select....
[textfield A][textfield B][textfield C]
and a confirm button, the user need to add three textfield, after that , the user need to click the confirm button.... but based on different select order, the result is different, for example:
A>B>C, I will show red.
When the user select in this order:
B>A>C I will show green.
When the user select in this order:
C>B>A I will show the color blue....
based on different user select order, it will show different color....
But the question is, when I add more and more textfield, how can I implement this logic?
First, I design to have an array , when the user select one textfield, I store the textfield id to array, when user select the second one, I will store in the array, until the user click confirm, I read back the array to display the color....
But I think it will become very big & messy when more and more textfield is added, any better ideas? Thank you.
It's a bit of a hack, but what I'd be inclined to do is store the selections in a string that gets appended to each time (starting with empty string of course), trimming to the rightmost x characters. Then you can do a simple switch/case statement to determine the color. For example (C# fragments, sort of):
string selectStr = string.Empty;
void Select(string btn) {
selectStr += btn;
selectStr = selectStr.Remove(0, btn.Length - 3);
}
void Confirm() {
switch (selectStr) {
case "ABC" : /* make red */ break;
case "BAC" : /* make green */ break;
// etc.
}
}