Retrieving value of elements inside a parser object in iOS - ios

I am working on a project which calls an API which returns an Xml data and inside my code classes are there to parse that data and extracting it to display on UI(NSXMLParser is used here).I am attaching the screen shot of xml response and the portion of code written to extract that parsed data(Parsing code I am not pasting here)
Code for extraction:
else if ([adType isEqualToString:#"textAd"])
{
NSString *html = [xml.documentRoot getNamedChild:#"htmlString"].text;
CGSize bannerSize;
if(adspaceHeight > 0 && adspaceWidth > 0)
{
bannerSize = CGSizeMake(adspaceWidth, adspaceHeight);
}
else if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad)
{
bannerSize = CGSizeMake(728, 90);
}
else
{
bannerSize = CGSizeMake(320, 50);
}
UIWebView *webView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, bannerSize.width, bannerSize.height)];
//load HTML string later (to avoid calling impression pixels when using custom events)
if(!headers) { //means that it's an interstitial ad
_htmlString = [NSString stringWithFormat: #"<style>*
{ -webkit-tap-highlight-color: rgba(0,0,0,0);} body {height:100%%; width:100%%;
}
img {max-width:100%%;
max-height:100%%; width:auto; height:auto; position: absolute; margin: auto; top: 0; left: 0; right: 0; bottom: 0;}
</style>
%#",html];
} else {
_htmlString = html;
}
if([skipOverlay isEqualToString:#"1"]) {
wasUserAction = NO;
webView.delegate = (id)self;
webView.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(handleTapGesture:)];
[webView addGestureRecognizer:tap];
tap.delegate = self;
} else {
webView.delegate = nil;
webView.userInteractionEnabled = NO;
// add overlay later, only if no custom event is shown
}
webView.backgroundColor = [UIColor clearColor];
webView.opaque = NO;
webView.scrollView.scrollsToTop = false;
self.bannerView = webView;
}
Now for me I want to modify the same code for another xml structure...
I am pasting the screenshot of response that I have to work on.Please help me to retrieve the data inside each node.(Especially there are two links inside the "ads" tag.How to retrieve them separately).

Related

iOS: the function (IBAction)inputChanged:(id)sender is very slow when displaying a tableview with the result of the search

I added a field, and when I tap the firsts characters, it shows me the result of the query in a sqlite, directly in a tableview generated just behind the field. The problem, is that it is very slow.
- (IBAction)inputChanged:(id)sender
{
dispatch_async(dispatch_get_main_queue(), ^(void){
[self.tableView removeFromSuperview];
if ([[self.searchLabel text] length] >= 2)
{
self->_results = [[searchDB database] searchDBResults:[self.searchLabel text]];
self.results = self->_results;
if (self.results.count > 0)
{
//tableview
self->_tableView = [[UITableView alloc] init];
if (IS_IPAD_DEVICE)
{
self->_tableView.frame = CGRectMake(self->searchLabel.frame.origin.x, 320, self->viewPreview.frame.origin.x - 40, self->statisticView.frame.origin.y - 430);
}
else
{
self->_tableView.frame = CGRectMake(self->searchLabel.frame.origin.x, self->searchLabel.frame.origin.y + 44, self.view.frame.size.width - 12, self->statisticView.frame.origin.y + 80);
}
self->_tableView.backgroundColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:0.35];
self->_tableView.dataSource = self;
self->_tableView.delegate = self;
self->_tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
[self.tableView reloadData];
[self.view addSubview:self.tableView];
}
}
});
}
Any suggestion ?

How to change multiple UILabel text colour while scrolling inside scrollview Objective-c iOS

Hi I am new to iOS development..Can any one help me..
I have added multiple UILabel inside UIScrollview based on array count..For example if array count is 3 means ..then In scrollview adding 3 view along with UILabel in each view..
So now 3 view having 3 different UILabels..
But now I want to change colour of UILabel text in different views based on requirement..but I am not able to update colour…
Its change colour of UILabel text only for last index ..
I have written code in ScrollViewDidScroll:(UIScrollView *)scrollView
Any suggestion ..
self.robotScrollView.contentSize = CGSizeMake(robotCounts*Robot_ScrollView_Width, kRobotSrollViewH);
for (int i=0; i<robotCounts; i++) {
self.robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];
self.robotLabel.textAlignment = NSTextAlignmentCenter;
self.robotLabel.backgroundColor = [UIColor clearColor];
self.robotLabel.textColor = [UIColor blackColor];
[self.robotScrollView addSubview:self.robotLabel];
if (kRemoteManager.robotsArray.count == 0) {
self.robotLabel.text = #"";
break;
}
DeviceBase *robot = kRemoteManager.robotsArray[i];
self.robotLabel.text = [NSString stringWithFormat:#"%#",robot.dName];
}
self.robotScrollView.contentOffset = CGPointMake(Robot_ScrollView_Width*currentRobotIndex, 0);
Changing UIlabeltext color in below scrollviewdidscroll method
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView == self.scrollView)
{
self.pageControl.currentPage = (scrollView.contentOffset.x+(scrollView.width/2)) / scrollView.width;
kRemoteManager.currentIndex = self.pageControl.currentPage;
[[NSNotificationCenter defaultCenter] postNotificationName:kNotify_Steward_ReloadData object:nil];
NSMutableArray *viewRemoteIDarray = [NSMutableArray array];
CardRemoteModel *model = kRemoteManager.cardRemoteArray[self.pageControl.currentPage];
if (![viewRemoteIDarray containsObject:#(model.remoteID)])
{
if (model.remoteType == kooKong_remoteType_AirCleaner || model.remoteType == kooKong_remoteType_AC)
{
self.robotLabel.textColor = [UIColor whiteColor];
}
else
{
self.robotLabel.textColor = [UIColor blackColor];
}
}
} else if (scrollView == self.robotScrollView) {
kRemoteManager.currentRobotIndex = (scrollView.contentOffset.x+(scrollView.width/2)) / scrollView.width;
self.leftBtn.hidden = NO;
self.rightBtn.hidden = NO;
if (kRemoteManager.currentRobotIndex == kRemoteManager.robotsArray.count-1) {
self.rightBtn.hidden = YES;
}
if (kRemoteManager.currentRobotIndex == 0){
self.leftBtn.hidden = YES;
}
}
}
There are three labels, but only one label property. The last one assigned is the only one you'll have access to later. One solution would be to keep an array of labels in the view controller.
#property(nonatomic, strong) NSMutableArray *labels;
In the posted method...
self.labels = [NSMutableArray array];
for (int i=0; i<robotCounts; i++) {
UILabel *robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];
[self.labels addObject:robotLabel]; // <--- new
robotLabel.textAlignment = NSTextAlignmentCenter;
robotLabel.backgroundColor = [UIColor clearColor];
robotLabel.textColor = [UIColor blackColor];
[self.robotScrollView addSubview:robotLabel];
if (kRemoteManager.robotsArray.count == 0) {
robotLabel.text = #"当前无酷控机器人";
break;
}
DeviceBase *robot = kRemoteManager.robotsArray[i];
robotLabel.text = [NSString stringWithFormat:#"%#",robot.dName];
}
To change all of the colors:
- (void)setLabelColors:(UIColor *)color {
for (UILabel *label in self.labels) {
label.textColor = color;
}
}
Another idea would be to give each label a tag, and find them when you need them.
for (int i=0; i<robotCounts; i++) {
self.robotLabel = [[UILabel alloc] initWithFrame:CGRectMake(Robot_ScrollView_Width*i, 0 , Robot_ScrollView_Width, kRobotSrollViewH)];
self.robotLabel.tag = i+1;
// the remainder of this loop as you have it
To change all the colors...
- (void)setLabelColors:(UIColor *)color {
for (int i=0; i<robotCounts; i++) {
UILabel *label = (UILabel *)[self.robotScrollView viewWithTag:i+1];
label.textColor = color;
}
}

UIDropInteractionDelegate not being called

I have a DropAreaViewController in my app. Inside this controller I run the following code:
- (void)viewDidLoad {
[super viewDidLoad];
UIDropInteraction *interaction = [[UIDropInteraction alloc] initWithDelegate:self];
[self.view addInteraction:interaction];
}
- (BOOL)dropInteraction:(UIDropInteraction *)dropInteraction canHandleSession:(nonnull id<UIDropSession>)session {
NSLog(#"canHandleSession called");
return true;
}
- (UIDropProposal *)dropInteraction:(UIDropInteraction *)dropInteraction sessionDidUpdate:(nonnull id<UIDropSession>)session {
NSLog(#"sessionDidUpdate");
if (session.localDragSession != nil) {
NSLog(#"is localDragSession");
return [[UITableViewDropProposal alloc] initWithDropOperation:UIDropOperationMove intent:UITableViewDropIntentInsertAtDestinationIndexPath];
} else {
return [[UITableViewDropProposal alloc] initWithDropOperation:UIDropOperationCancel];
}
}
- (void)dropInteraction:(UIDropInteraction *)interaction performDrop:(id<UIDropSession>)session {
self.view.backgroundColor = [UIColor redColor];
}
Now, in another viewcontroller I add this controller:
DropAreaViewController *davc = [[DropAreaViewController alloc] init];
davc.view.frame = CGRectMake(0, 0, 100, 100);
[self.view addSubview:davc.view];
None of the drops are being registered though. I get not logs at all when I hover a drop on top of the square. Any ideas?

Rotate a PDF from InAppBrowser

So I was able to integrate InAppBrowser-CE this plugin which works amazingly to display PDF's from a specified window size and give transparency, but now I am trying to figure out how to rotate the PDF. I want to add a rotation tool (similar to the photo library in iOS) but dont know where to start at. I saw something about image rotation tool, but wasnt sure if it applied to a pdf file, and I don't want to keep at something that doesn't work (i.e. using iFrame instead of InAppBrowser to display PDFs) any help is much appreciated. Here is just a sample of how the PDF is called to give you some insight.
This is from HTML to call it to open:
<button onclick="window.open('http://www.free.woodworking-plans.org/images/ranch-house-7161/ranch-house-floor-plan-o.jpg','_blank','vw=685,vh=650,vx=40,vy=40');">Change my size</button>
And here is how the PDF is opened (from xcode CDVInAppBrowser.m)
- (void)openInInAppBrowser:(NSURL*)url withOptions:(NSString*)options
{
if (self.inAppBrowserViewController == nil) {
NSString* originalUA = [CDVUserAgentUtil originalUserAgent];
self.inAppBrowserViewController = [[CDVInAppBrowserViewController alloc] initWithUserAgent:originalUA prevUserAgent:[self.commandDelegate userAgent]];
self.inAppBrowserViewController.navigationDelegate = self;
if ([self.viewController conformsToProtocol:#protocol(CDVScreenOrientationDelegate)]) {
self.inAppBrowserViewController.orientationDelegate = (UIViewController <CDVScreenOrientationDelegate>*)self.viewController;
}
}
// set pointer to this viewcontroller for later use
iabvc = self.inAppBrowserViewController;
CDVInAppBrowserOptions* browserOptions = [CDVInAppBrowserOptions parseOptions:options];
[self.inAppBrowserViewController showLocationBar:browserOptions.location];
[self.inAppBrowserViewController showToolBar:browserOptions.toolbar];
....
- (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent
{
self = [super init];
if (self != nil) {
_userAgent = userAgent;
_prevUserAgent = prevUserAgent;
_webViewDelegate = [[CDVWebViewDelegate alloc] initWithDelegate:self];
[self createViews];
}
return self;
}
And here is what the tools section looks like,
self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0, (self.view.bounds.size.height - TOOLBAR_HEIGHT), self.view.bounds.size.width, TOOLBAR_HEIGHT)];
self.toolbar.alpha = 1.000;
self.toolbar.autoresizesSubviews = YES;
self.toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
self.toolbar.barStyle = UIBarStyleBlackOpaque;
self.toolbar.clearsContextBeforeDrawing = NO;
self.toolbar.clipsToBounds = NO;
self.toolbar.contentMode = UIViewContentModeScaleToFill;
self.toolbar.contentStretch = CGRectFromString(#"{{0, 0}, {1, 1}}");
self.toolbar.hidden = NO;
self.toolbar.multipleTouchEnabled = NO;
self.toolbar.opaque = NO;
self.toolbar.userInteractionEnabled = YES;
I created a rotate button then added this below,
.h
//header - add 'int counter'
#interface CDVInAppBrowserViewController : UIViewController <UIWebViewDelegate>{
#private
NSString* _userAgent;
NSString* _prevUserAgent;
NSInteger _userAgentLockToken;
CDVWebViewDelegate* _webViewDelegate;
int counter;
}
.m
//Add this to the top of the .m file
#define DegreesToRadians(X) ((X) * M_PI / 180.0)
...
//reference
- (id)initWithUserAgent:(NSString*)userAgent prevUserAgent:(NSString*)prevUserAgent
{
counter = 0;
...
//create Rotate Button
NSString* rotateString = #"Rotate";
self.RotateButton = [[UIBarButtonItem alloc] initWithTitle:rotateString style:UIBarButtonItemStylePlain target:self action:#selector(goRotate:)];
self.RotateButton.enabled = YES;
self.RotateButton.imageInsets = UIEdgeInsetsZero;
....
//action - sender
-(IBAction)goRotate:(id)sender
{ counter ++; float angle = (counter%3)*90;
[UIView beginAnimations:#"rotate" context:nil];
[UIView setAnimationDuration:0.5];
self.webView.transform = CGAffineTransformMakeRotation(DegreesToRadians(angle));
[UIView commitAnimations];
}

why i change the alpha value of UIImageView but no effect?

I am working on a reader app. when you read one magazine, it will display first five pages and download the rest pages one by one. there is a scrollview to view the thumbnail image of pages. At the beginning, if the page needs downloading, the corresponding thumbnail view's alpha value is set to 0.5 (the thumbnail images are in the file,no need to download). when the page is downloaded, i will update the thumbnail view's value to 1.0. I use one operation to download the page, and when one is downloaded i use delegate to set thumbnail view's alpha.
But when i update thumbnail view's alpha value, it still the same as the beginning. it seems the alpha has no effect. I wonder is there anything wrong with my code? some snippets are as follows:
In the PageViewController.m
- (void)loadView
{
[super loadView];
//...
[self createSlideUpViewIfNecessary];
[self downloadPages];
}
- (void)createSlideUpViewIfNecessary {
if (!slideUpView) {
[self createThumbScrollViewIfNecessary];
// create container view that will hold scroll view and label
CGRect frame = CGRectMake(CGRectGetMinX(self.view.bounds), CGRectGetMaxY(self.view.bounds), CGRectGetWidth(self.view.bounds), CGRectGetHeight(thumbScrollView.frame));
slideUpView = [[UIView alloc] initWithFrame:frame];
[slideUpView setBackgroundColor:[UIColor blackColor]];
[slideUpView setOpaque:NO];
[slideUpView setAlpha:0.75];
[[self view] addSubview:slideUpView];
// add subviews to container view
[slideUpView addSubview:thumbScrollView];
}
}
- (void)createThumbScrollViewIfNecessary {
if (!thumbScrollView) {
float scrollViewHeight = THUMB_HEIGHT + THUMB_V_PADDING;
float scrollViewWidth = CGRectGetWidth(self.view.bounds);
thumbScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, scrollViewWidth, scrollViewHeight)];
[thumbScrollView setCanCancelContentTouches:NO];
[thumbScrollView setClipsToBounds:NO];
// now place all the thumb views as subviews of the scroll view
// and in the course of doing so calculate the content width
float xPosition = THUMB_H_PADDING;
for (int i = 0; i < magazine.pageNum ; i++) {
Page *page = [magazine.pages objectAtIndex:i];
NSString *name = page.pageName;
NSString *mfjName =[name stringByReplacingOccurrencesOfString:#".mfj" withString:#"Small.mfj"];
UIImage *thumbImage = nil;
if([mfjName hasSuffix:#".mfj"])
thumbImage = [Reader loadMfjFromSprOrCache:magazine MFJ:mfjName];
ThumbImageView *thumbView;
if (thumbImage) {// sometimes mfjname is 0 which means white page in normal and black thumbnail in thumbnail scrollview.
if (!mThumbnailSizeUpdated) {
mThumbnailWidth = thumbImage.size.width;
mThumbnailHeight = thumbImage.size.height;
mThumbnailSizeUpdated = YES;
}
thumbView = [[ThumbImageView alloc] initWithImage:thumbImage];
} else {
CGRect thumbFrame;
if (mThumbnailSizeUpdated) {
thumbFrame = CGRectMake(0, 0, mThumbnailWidth, mThumbnailHeight);
} else {
mThumbnailWidth = 80;
mThumbnailHeight = 100;
thumbFrame = CGRectMake(0, 0, mThumbnailWidth, mThumbnailHeight);
}
thumbView = [[ThumbImageView alloc] initWithFrame:thumbFrame];
}
NSString *mfjPath= [[magazine getDownloadPath] stringByAppendingPathComponent:name];
if (![magazine getFileInfo:name]&&![[NSFileManager defaultManager] fileExistsAtPath:mfjPath]) {
thumbView.alpha = 0.5;
}
[thumbView setBackgroundColor:[UIColor blackColor]];
[thumbView setTag:THUMBVIEW_OFFSET+i];
[thumbView setDelegate:self];
[thumbView setImageName:name];
CGRect frame = [thumbView frame];
frame.origin.y = THUMB_V_PADDING;
frame.origin.x = xPosition;
frame.size.width = frame.size.width+30;
frame.size.height = frame.size.height+40;
[thumbView setFrame:frame];
[thumbScrollView addSubview:thumbView];
UILabel *pageIndexLabel = [[UILabel alloc] initWithFrame:CGRectMake(xPosition, frame.origin.y+frame.size.height-THUMB_LABEL_HEIGHT, frame.size.width, THUMB_LABEL_HEIGHT)];
[pageIndexLabel setBackgroundColor:[UIColor clearColor]];
[pageIndexLabel setText:[NSString stringWithFormat:#"%d",(i+1)]];
[pageIndexLabel setTextColor:[UIColor whiteColor]];
[pageIndexLabel setTextAlignment:UITextAlignmentCenter];
[thumbScrollView addSubview:pageIndexLabel];
xPosition += (frame.size.width + THUMB_H_PADDING);
}
thumbScrollView.showsHorizontalScrollIndicator = NO;
[thumbScrollView setContentSize:CGSizeMake(xPosition, scrollViewHeight)];
}
}
- (void)downloadPages
{
DownloadOperation *op = [[DownloadOperation alloc] initWithMagazine:magazine];
op.delegate = self;
[[(AppDelegate *)[[UIApplication sharedApplication] delegate] sharedOperationQueue] addOperation:op];
}
- (void)downloadOperation:(DownloadOperation *)operation finishedAtIndex:(NSUInteger)index
{
if (thumbScrollView){
[thumbScrollView viewWithTag:THUMBVIEW_OFFSET+index].alpha = 1.0;
}
}
In DownloadOperation.m
- (void)main
{
// ...
NSUInteger index = 0;
for (Page *page in mMagazine.pages)
{
if (/*page doesn't exist*/){
// download the page;
if ([delegate respondsToSelector:#selector(downloadOperation:finishedAtIndex:)]) {
[delegate downloadOperation:self finishedAtIndex:index];
}
}
index++;
}
}
you're using operation queue to download images -> thus, your finish callbacks might arrive not on the main thread, but you are trying to update you UI anyway - try wrapping your UI interaction into dispatch_async on main thread:
dispatch_async(dispatch_get_main_queue), ^{
if (thumbScrollView){
[thumbScrollView viewWithTag:THUMBVIEW_OFFSET+index].alpha = 1.0;
}
});
Thanks to #Inafziger for clues on this.

Resources