I have a template quiz application that has been on the app store in various guises for a while now, in general it receives good reviews and had no bug reports.
Recently I've had two bug reports, people using iPads with iOS 8.1.2 or 8.1.3, saying that now and again the UITextView that I use to show the questions is blank.
I've not been able to replicate this bug, but I would be grateful if someone could shed some light on it.
The objects questions and userAnswer are not nil so it is definitely a UITextView issue.
The Controller is here:
#interface ReviewController ()
#property (strong, nonatomic) IBOutlet UITextView *output;
#end
#implementation ReviewController
- (void)viewDidLoad {
[super viewDidLoad];
[self setReviewText];
// Do any additional setup after loading the view.
}
-(void)setReviewText{
NSArray* questions = [[NSArray alloc] init];
questions = [_manager getAllQuestions];
NSMutableArray* userAnswer = [[NSMutableArray alloc] init];
userAnswer = [self answersAttributed];
if(questions == nil)
_output.text = #"Questions Error";
if (userAnswer == nil)
{
_output.text = #"Error";
}
NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] init];
int i = 0;
NSAttributedString *linebreak =[[NSAttributedString alloc] initWithString:#"\n"];
for (Question* q in questions)
{
if (i == [userAnswer count])
break;
NSAttributedString *qtext =[[NSAttributedString alloc] initWithString:q.questionText];
NSAttributedString *ctext =[[NSAttributedString alloc] initWithString:#"Correct Answer:\n"];
NSAttributedString *atext =[[NSAttributedString alloc] initWithString:q.answerText];
NSAttributedString *ytext =[[NSAttributedString alloc] initWithString:#"Your Answer:\n"];
NSAttributedString *utext =[[NSAttributedString alloc] initWithAttributedString:userAnswer[i]];
[mutableAttString appendAttributedString:qtext];
[mutableAttString appendAttributedString:linebreak];
[mutableAttString appendAttributedString:ctext];
[mutableAttString appendAttributedString:atext];
[mutableAttString appendAttributedString:linebreak];
[mutableAttString appendAttributedString:ytext];
[mutableAttString appendAttributedString:utext];
[mutableAttString appendAttributedString:linebreak];
[mutableAttString appendAttributedString:linebreak];
i++;
}
NSAttributedString* outText = [[NSAttributedString alloc] init];
outText = [mutableAttString mutableCopy];
_output.attributedText = outText;
[_output setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
}
-(NSMutableArray*)answersAttributed
{
NSMutableArray* ansAtt = [[NSMutableArray alloc] init];
NSArray* questions = [[NSArray alloc] init];
questions = [_manager getAllQuestions];
NSArray* userAnswers = [[NSArray alloc] init];
userAnswers = [_manager getAllUserAnswers];
if ([questions count] != [userAnswers count])
{
return ansAtt;
}
int i = 0;
for (NSString* userAnswer in userAnswers )
{
if([[questions[i] answerText] isEqualToString:userAnswer] )
{
NSDictionary *attributes = #{NSBackgroundColorAttributeName:[UIColor greenColor]};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:userAnswer attributes:attributes];
[ansAtt addObject:attrString];
}
else
{
NSDictionary *attributes = #{NSBackgroundColorAttributeName:[UIColor redColor]};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:userAnswer attributes:attributes];
[ansAtt addObject:attrString];
}
i++;
}
return ansAtt;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"review2results"])
{
// Get reference to the destination view controller
ReviewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setManager:_manager];
}
}
With autolayout, if you don't have enough constraints to fully specify size and position, then sometimes you'll see and and sometimes you won't. The unspecified constraints can have random values, like 0 height or off screen position.
Check to make sure the constraints are fully specified.
In my case textview marked as Non-Editable and Non-Selectable in Storyboard have the same strange behavior on iOS 8.1 (no problems in iOS 9+).
- (void)viewDidLoad {
[super viewDidLoad];
textview.attributedText = [[NSAttributedString alloc] initWithString:#"non-nil"]];
value = textview.attributedText;
//^ value is nil !!!
}
Fixed with this:
- (void)viewDidLoad {
[super viewDidLoad];
textview.editable = YES;
textview.attributedText = [[NSAttributedString alloc] initWithString:#"non-nil"]];
textview.editable = NO;
value = textview.attributedText;
//^ value is #"non-nil" now !!!
}
Related
I am trying to create a custom label for coreplot using NSAttributedString. Everything works fine when my strings have 1 line.
The problem occurs when I want to have 2 line strings using code:
NSMutableAttributedString* remFinalLabel = [remAttrStr mutableCopy];
NSMutableAttributedString *newLineAttrStr = [[NSMutableAttributedString alloc]initWithString:#"\n"];
[remFinalLabel appendAttributedString:newLineAttrStr];
[remFinalLabel appendAttributedString:rem2AttrStr];
The result looks like that: not even first string is properly displayed. How can I set the number of lines in custom label?
My code to create labels:
NSMutableArray* labelsStrings = [NSMutableArray arrayWithObjects:strAttr1, strAttr2, strAttr3, nil];
NSMutableArray *ticksLocations = [NSMutableArray arrayWithObjects:#0.5, #1.5, #2.5, nil];
NSMutableArray* customLabels = [[NSMutableArray alloc] init];
NSUInteger labelLocation = 0;
#try {
for (NSNumber *tickLocation in ticksLocations) {
NSAttributedString* currentLabel = [labelsStrings objectAtIndex:labelLocation];
CPTTextLayer *txtLayer = [[CPTTextLayer alloc] initWithAttributedText:currentLabel];
CPTAxisLabel* newLabel = [[CPTAxisLabel alloc] initWithContentLayer:txtLayer];
newLabel.tickLocation = tickLocation;
newLabel.offset = 0.0f;
newLabel.alignment = CPTAlignmentLeft;
[customLabels addObject:newLabel];
labelLocation++;
}
}
#catch (NSException * e) {
DLog(#"An exception occurred while creating date labels for x-axis");
}
#finally {
y.axisLabels = [NSSet setWithArray:customLabels];
}
y.majorTickLocations = [NSSet setWithArray:ticksLocations];
You can achieve this by setting the frame of content layer of your CPTAxisLabel.
Try this:-
CPTAxisLabel *label = [[CPTAxisLabel alloc] initWithText:#"Your Text Here" textStyle:#"Text Style"];
[label.contentLayer setFrame:CGRectMake(0, 0, 40, 40)]; //change this frame according to your requirement
Happy Coding..!!
I have a custom UILabel that I download from Github (called KILabel). The reason I got it because I wanted to duplicate Instagram's activity feed(The segmented control and the UITableViews with 2 different sets of information). In instagram's app I know there is the ability to tap on people usernames within the label that is contained in each and ever UITableViewCell(This is where KILabel comes into play). KILabel recognizes "#usernames", "#hashtags", and URLs and makes them tappable almost like a UIButton.
I had previously went without the KILabel and used the regular UILabel and it was able to expand in iOS 8 and iOS 7. Now, when I use the KILabel it auto expands(Dynamically changes its height to fit the text) ONLY on iOS 7 and it does NOT start off expanded. When I scroll in my UITableViewController, that is when the label expands. It starts off normal height.
As of right now this what the Custom Cell looks like
#import <UIKit/UIKit.h>
#import "KILabel.h"
#interface NewsCell : UITableViewCell
#property (strong, nonatomic) IBOutlet KILabel *cellLabel;
#property (strong, nonatomic) IBOutlet UIImageView *cellImageView;
#end
I have the label in the interface(storyboard) hooked up to the custom KILabel class as well as the ImageView.
Next I have the cell being implemented in the UITableViewController. I tested it with a large piece of text to see if the cell and label would expand to the correct size
cell = [self.tableView dequeueReusableCellWithIdentifier:cellIdentifier2 forIndexPath:indexPath];
cell.cellImageView.layer.cornerRadius = 6.0f;
cell.cellImageView.clipsToBounds = YES;
cell.cellImageView.layer.borderWidth = 1.0f;
cell.cellImageView.layer.borderColor = [[UIColor blackColor] CGColor];
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] init];
longPress.numberOfTapsRequired = 1;
[cell.cellLabel addGestureRecognizer:longPress];
//Create the cell label going into the cell
cell.cellLabel.linkTapHandler = ^(KILinkType linkType, NSString *string, NSRange range) {
NSString *mString = [string stringByReplacingOccurrencesOfString:#"#" withString:#""];
if (linkType == KILinkTypeURL) {
// Open URLs
//[self attemptOpenURL:[NSURL URLWithString:string]];
} else if (linkType == KILinkTypeUserHandle) {
if (longPress.state == UIGestureRecognizerStatePossible) {
[self tapOnUsername:longPress username:mString];
}
} else {
// Put up an alert with a message if it's not an URL
NSString *linkTypeString = #"Username";
if (linkType == KILinkTypeHashtag)
{
linkTypeString = #"Hashtag";
}
NSString *message = [NSString stringWithFormat:#"You tapped %# which is a %#", string, linkTypeString];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Hello"
message:message
delegate:nil
cancelButtonTitle:#"Dismiss"
otherButtonTitles:nil];
[alert show];
}
};
PFObject *eachNews = [self.followingNews objectAtIndex:indexPath.row];
PFUser *notifier = [eachNews objectForKey:#"Notifier"];
PFUser *notified = [eachNews objectForKey:#"Notified"];
[notifier fetchIfNeeded];
[notified fetchIfNeeded];
NSString *notifierString = [[NSString alloc] init];
NSString *notifiedString = [[NSString alloc] init];
NSString *grammer = [[NSString alloc] init];
NSDate *timeStamp = eachNews.createdAt;
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MMM d, yyyy h:mm a"];
NSString *timeString = [timeStamp formattedAsTimeAgo];
if ([notifier.username isEqualToString:_loggedInUser.username]) {
notifierString = #"You";
grammer = #"are";
} else {
notifierString = [NSString stringWithFormat:#"#%#", notifier.username];
grammer = #"is";
}
if ([notified.username isEqualToString: _loggedInUser.username]) {
notifiedString = #"you";
} else {
notifiedString = [NSString stringWithFormat:#"#%#", notified.username];
}
if (notifier[#"profileImage"] == nil) {
UIImage *hermet = [UIImage imageNamed:#"user"];
[cell.cellImageView setImage:hermet];
} else {
PFFile *imageFile = notifier[#"profileImage"];
[cell.cellImageView setImage:[UIImage imageWithData:[imageFile getData]]];
}
NSMutableString *newsText = [[NSMutableString alloc] init];
if ([eachNews[#"Type"] isEqualToString:#"Follow"]) {
[newsText appendString:[NSString stringWithFormat:#"%# aaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUVaaaaaaaaaaaaaaasdasafsfsdgsdgsdfgsdfsodjnfsaioefgnarpuoigbweuifbsdpugbdsfiougbsdiugosbdgiusobfasdioFPBSADUBVSIUDVBSAIUDBSDIUVBSDUVIBDSFIUVBSDIUVSBAVIPUBDSIUV%# %#.\n", notifierString, eachNews[#"Messages"], notifiedString]];
} else if ([eachNews[#"Type"] isEqualToString:#"New Founder"]) {
[newsText appendString:[NSString stringWithFormat:#"%# %# %#.\n", notifierString, grammer, eachNews[#"Messages"]]];
} else if ([eachNews[#"Type"] isEqualToString:#"Club Create"]) {
if([notified.username isEqualToString:_loggedInUser.username]) {
notifiedString = #"You";
} else {
notifiedString = notified.username;
}
if (notified[#"profileImage"] == nil) {
UIImage *hermet = [UIImage imageNamed:#"user"];
[cell.cellImageView setImage:hermet];
} else {
PFFile *imageFile = notified[#"profileImage"];
[cell.cellImageView setImage:[UIImage imageWithData:[imageFile getData]]];
}
[newsText appendString:[NSString stringWithFormat:#"%# %#\n", notifiedString, eachNews[#"Messages"]]];
}
[newsText appendString:timeString];
NSArray *appendedString = [newsText componentsSeparatedByString:#"\n"];
NSRange dateRange = [newsText rangeOfString:appendedString[1]];
NSMutableAttributedString *attrString = [[NSMutableAttributedString alloc] initWithString:newsText];
[attrString beginEditing];
[attrString addAttribute: NSForegroundColorAttributeName
value:[UIColor lightGrayColor]
range:dateRange];
[attrString endEditing];
cell.cellLabel.attributedText = attrString;
CGSize sizeForLabel = CGSizeMake(cell.cellLabel.frame.size.width, 0);
CGRect labelRect = CGRectMake(cell.cellLabel.frame.origin.x, cell.cellLabel.frame.origin.y, sizeForLabel.width, CGFLOAT_MAX);
// Use a dummy label and its textRectForBounds method to calculate the height
// of a real label.
KILabel *measureLabel = [[KILabel alloc] initWithFrame:CGRectMake(cell.cellLabel.frame.origin.x, cell.cellLabel.frame.origin.y, labelRect.size.width, 0)];
measureLabel.numberOfLines = 0;
measureLabel.attributedText = attrString;
labelRect = [measureLabel textRectForBounds:labelRect limitedToNumberOfLines:0];
cell.cellLabel.frame = labelRect;
The height of UITableViewCell is completely fine and expands perfectly. The label is the only issues. It does not expand at all on iOS 8 and expands ONLY AFTER I have scrolled in the UITableViewController on iOS 7.
I would tagged KILabel but I can't and if anyone has used another UILabels like KILabel and been able to get them auto-expand please refer me to them.
KILabel on GitHub: https://github.com/Krelborn/KILabel
Also, I have already tried the Gist the creator posted on his GitHub and that will get my cell to expand perfectly.
I have a revision quiz app in the App store that has been receiving around 10 downloads per day for the last 3 months. It's quite well used and for the most part has good reviews. However, recently I've had two 1 star reviews because people are complaining that the "Review" screen is blank after the quiz ends.
I've tried this on iPhone 5/5c/5s and it works perfectly well. These are the only devices I have access to, but I've also run the simulator for iPad and both iPhone 6 versions and I can't replicate the problem.
The flow is RootController->QuizController->ResultsController->ReviewController. The quiz controller has an object called GameManager as an instance variable. This tracks the user answers and questions as the quiz progresses, and is passed from the Quiz to the Results and then to the Review section during segues.
The functions that pass the object are:
//From Quiz to Results
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"quiz2results"])
{
// Get reference to the destination view controller
ResultsController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setManager:_manager];
}
}
//From Results to Review
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:#"results2review"])
{
// Get reference to the destination view controller
ReviewController *vc = [segue destinationViewController];
// Pass any objects to the view controller here, like...
[vc setManager:_manager];
}
}
Then in the Review controller the following code is executed:
-(void)setReviewText{
NSArray* questions = [[NSArray alloc] init];
questions = [_manager getAllQuestions];
NSMutableArray* userAnswer = [[NSMutableArray alloc] init];
userAnswer = [self answersAttributed];
if (userAnswer == nil)
{
_output.text = #"Error";
}
NSMutableAttributedString *mutableAttString = [[NSMutableAttributedString alloc] init];
int i = 0;
NSAttributedString *linebreak =[[NSAttributedString alloc] initWithString:#"\n"];
for (Question* q in questions)
{
if (i == [userAnswer count])
break;
NSAttributedString *qtext =[[NSAttributedString alloc] initWithString:q.questionText];
NSAttributedString *ctext =[[NSAttributedString alloc] initWithString:#"Correct Answer:\n"];
NSAttributedString *atext =[[NSAttributedString alloc] initWithString:q.answerText];
NSAttributedString *ytext =[[NSAttributedString alloc] initWithString:#"Your Answer:\n"];
NSAttributedString *utext =[[NSAttributedString alloc] initWithAttributedString:userAnswer[i]];
[mutableAttString appendAttributedString:qtext];
[mutableAttString appendAttributedString:linebreak];
[mutableAttString appendAttributedString:ctext];
[mutableAttString appendAttributedString:atext];
[mutableAttString appendAttributedString:linebreak];
[mutableAttString appendAttributedString:ytext];
[mutableAttString appendAttributedString:utext];
[mutableAttString appendAttributedString:linebreak];
[mutableAttString appendAttributedString:linebreak];
i++;
}
NSAttributedString* outText = [[NSAttributedString alloc] init];
outText = [mutableAttString mutableCopy];
_output.attributedText = outText;
[_output setFont:[UIFont preferredFontForTextStyle:UIFontTextStyleBody]];
}
-(NSMutableArray*)answersAttributed
{
NSMutableArray* ansAtt = [[NSMutableArray alloc] init];
NSArray* questions = [[NSArray alloc] init];
questions = [_manager getAllQuestions];
NSArray* userAnswers = [[NSArray alloc] init];
userAnswers = [_manager getAllUserAnswers];
if ([questions count] != [userAnswers count])
{
return ansAtt;
}
int i = 0;
for (NSString* userAnswer in userAnswers )
{
if([[questions[i] answerText] isEqualToString:userAnswer] )
{
NSDictionary *attributes = #{NSBackgroundColorAttributeName:[UIColor greenColor]};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:userAnswer attributes:attributes];
[ansAtt addObject:attrString];
}
else
{
NSDictionary *attributes = #{NSBackgroundColorAttributeName:[UIColor redColor]};
NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:userAnswer attributes:attributes];
[ansAtt addObject:attrString];
}
i++;
}
return ansAtt;
}
If anybody could shed some light on this I'd be delighted. The cause of this problem has alluded me so far.
Now I create is as follow (my file.h):
UIImageView *pic1, *pic2, *pic3, *pic4, *pic5, *pic6, *pic7, *pic8, *pic9, *pic10;
Then in my (file.m):
UIImageView *pic1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#”picName.png”]];
UIImageView *pic2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#”picName.png”]];
……
UIImageView *pic10 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#”picName.png”]];
I need many instances of UIImageView (number triggered by other factors) of only one in this case picture.
Is there any way to create multiple instances of UIImageView automatically in my file.m, somehow as follow?:
for (int x; (x=10); x++)
{
UIImageView * pic[x] = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myPic.png"]];
}
This example doesn’t work but I’d like to show what I want to program.
Of course you can - that is what the arrays are for:
NSMutableArray *pics = [NSMutableArray array];
for (int i = 0 ; i != 10 ; i++) {
[pics addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"myPic.png"]]];
}
In case the name of the picture depends on the index, use NSString's stringWithFormat to produce the name of the picture - for example, you can do it like this:
NSMutableArray *pics = [NSMutableArray array];
for (int i = 0 ; i != 10 ; i++) {
NSString *imgName = [NSString stringWithFormat:#"myPic%d.png"];
[pics addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:imgName]]];
}
If the names of images follow a standard pattern you can just loop over the required amount and build the image name dynamically using the index.
If the names of the images do not follow a standard patten you can chuck them in an array and loop over them
NSArray *imageNames = #[
#"alarm.jpg",
#"bell.jpg",
#"car.jpg"
];
NSMutableArray *imageViews = [[NSMutableArray alloc] init];
for (NSString *imageName in imageNames) {
[imagesViews addObject:({
[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
})];
}
Completely optional - further reading
You could write yourself a simple category that abstracts this looping and collecting the results into a new array. This would allow you to simply write
NSArray *imageNames = #[
#"alarm.jpg",
#"bell.jpg",
#"car.jpg"
];
NSArray *imageViews = [imageNames pas_arrayByMappingWithBlock:^(id obj){
return [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
}];
The category to do that would look something like this:
#interface NSArray (PASAdditions)
- (NSArray *)pas_arrayByMappingWithBlock:(id (^)(id obj))block
#end
#implementation NSArray (PASAdditions)
- (NSArray *)pas_arrayByMappingWithBlock:(id (^)(id obj))block
{
NSMutableArray *result = [[NSMutableArray alloc] init];
for (id obj in self) {
[result addObject:block(obj)];
}
return [result copy];
}
#end
I'm trying to implement a basic QuickDialog view and the placement of the elements works fine.
But when I select an element the view won't scroll it into view. I thought the library would do this by itself or am I missing something?
Here is my code:
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
QRootElement *root = [[QRootElement alloc] init];
root.title = #"";
root.grouped = YES;
self.root = root;
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
QSection *driverSection = [[QSection alloc] initWithTitle:#"xxx"];
QEntryElement *lblDriverSsn = [[QEntryElement alloc] initWithTitle:#"xxx" Value:#""];
lblDriverSsn.key = #"driverSsn";
QSection *vehicleSection = [[QSection alloc] initWithTitle:#"xxx"];
NSArray *component1 = #[#"xxx", #"xxx"];
QPickerElement *EventDescriptionPicker = [[QPickerElement alloc] initWithTitle:#"xxx" items:#[component1] value:#""];
EventDescriptionPicker.key = #"eventDescription";
NSMutableArray *speedValues = [[NSMutableArray alloc]init];
for (int i = 0; i <= 201; i+=5)
{
[speedValues addObject:[NSString stringWithFormat:#"%d%#", i,#" km/h"]];
}
QPickerElement *vehicleSpeed = [[QPickerElement alloc] initWithTitle:#"xxx" items:#[speedValues] value:#""];
self.damageReported = [[QBooleanElement alloc] initWithTitle:#"xxx" BoolValue:NO];
self.damageReported.onImage = [UIImage imageNamed:#"imgOn"];
self.damageReported.offImage = [UIImage imageNamed:#"imgOff"];
self.damageReported.controllerAction = #"showReportNumber:";
QEntryElement *lblReportNumber = [[QEntryElement alloc] initWithTitle:#"xxx" Value:#""];
lblDriverSsn.key = #"reportNumber";
//Sections
[driverSection addElement:lblDriverSsn];
[self.root addSection:driverSection];
[vehicleSection addElement:EventDescriptionPicker];
[vehicleSection addElement:vehicleSpeed];
[vehicleSection addElement:self.damageReported];
[vehicleSection addElement:lblReportNumber];
[self.root addSection:vehicleSection];
}
Am I missing something here?
Thanks in advance
On your initWithCoder: , set the following:
self.resizeWhenKeyboardPresented =YES;
This causes the view controller to listen for keyboard notifications , and adjust the inset accordingly.