English 中文(简体)
反向 UILabel. text 值
原标题:Reverse UILabel.text value

我有一个程序即将完成, 这个程序是一个名称生成器。 现在它从两个不同的阵列中取一个随机值, 将它们连接起来, 并显示结果作为标签 。 文本 -- 我想要有一个按钮来反转标签文本( 而不是 John Doe Doe, 它会说 Doe John ) 。 这是用来这样做的代码 。 我的想法是, 我从数组中使用相同的值, 然后将字符串指派给一个变量, 稍后在另一个名为“ 反名价” 或其它方法中使用 。 它不起作用 :) 当我单击“ 反名” 按钮时, 它会显示一个完全不同的名称 。 任何建议吗?

- (IBAction)generateBName:(id)sender {
int a = arc4random() % 3;
int b = arc4random() % 3;


// populate the array for the names
NSArray *firstNameArray = [NSArray arrayWithObjects: @"Jacob",
                           @"Ethan",
                           @"Justin", nil];
NSArray *middleNameArray = [NSArray arrayWithObjects: @"Jose",
                            @"Jeremiah",
                            @"Julian", nil];

// concatenate strings at index of array
NSString *fullName = [NSString stringWithFormat:@"%@ %@", [firstNameArray objectAtIndex:a], [middleNameArray objectAtIndex:b]];
NSString *reverseName = [NSString stringWithFormat:@"%@ %@", [firstNameArray objectAtIndex:b], [middleNameArray objectAtIndex:a]];

// display the newly created first & middle names
reverseNameString = reverseName;
babyname.text = fullName;

反向方法 :

- (IBAction)reverseLabel:(id)sender {
babyname.text = reverseNameString;
}
最佳回答

设置反向名字符串的线条,其名称向后,因此它会抓取一个第一个名字,然后取取一个中间名称,但使用反向索引。它应该是:

 NSString *reverseName = [NSString stringWithFormat:@"%@ %@", [middleNameArray objectAtIndex:b],[firstNameArray objectAtIndex:a]];
问题回答

暂无回答




相关问题
List Contents of Directory in a UITableView

I am trying to list the contents of Ringtones directory in a TableView, however, I am only getting the last file in the directory in ALL cells, instead of file per cell. This is my code: - (...

iPhone NSUserDefaults persistance difficulty

In my app i have a bunch of data i store in the NSUserdefaults. This information consists of an NSObject (Object1) with NSStrings and NSNumbers and also 2 instances of yet another object (Object2). ...

Writing a masked image to disk as a PNG file

Basically I m downloading images off of a webserver and then caching them to the disk, but before I do so I want to mask them. I m using the masking code everyone seems to point at which can be found ...

Resize UIImage with aspect ratio?

I m using this code to resize an image on the iPhone: CGRect screenRect = CGRectMake(0, 0, 320.0, 480.0); UIGraphicsBeginImageContext(screenRect.size); [value drawInRect:screenRect blendMode:...

Allowing interaction with a UIView under another UIView

Is there a simple way of allowing interaction with a button in a UIView that lies under another UIView - where there are no actual objects from the top UIView on top of the button? For instance, ...

热门标签