Dailycode.info

Short solution for short problems

iOS: Navigate back to root view controller then navigate away in one action.

There are several ways to reach this goal. The goal being to navigate from any view controller in the navigation controllers view controller collection, back to the root view controller and then forward to another controller. Why, because sometimes you want the back of you're new view controller to be the root view controller and not the previous controller. In my case I end an editing proces and the user has to be directed back to the edit template overview page. But when the user then pushes back, he needs to end up in the root view.

So here's the code, what we are doing it simple, just alter the list of view controller from the navigation controller:

        NSArray *viewControllers = senderVC.navigationController.viewControllers;

        NSMutableArray *newViewControllers = [NSMutableArray array];

        

        // preserve the root view controller

        [newViewControllers addObject:[viewControllers objectAtIndex:0]];

        // add the new view controller

        UIStoryboard * sb = [UIStoryboardstoryboardWithName:@"TemplatesStoryboard"bundle:nil];

        UIViewController * vcTo = [sb instantiateInitialViewController];

        [newViewControllers addObject:vcTo];

        // animatedly change the navigation stack 

        [senderVC.navigationController setViewControllers:newViewControllers animated:YES];

First we take the root view controller from the index 0, then add it to our newviewcontroller array. Next just init your second view, were you want the user to end up and add it to the newviewcontrollers array. Last we set the current view controller which has to be in the navigation controllers view controllers collection.

This piece of code is called from a top menu action, that is added dynamically to each view controller. This code does the trick!


.Net Web API and querystrings

 

Creating a web api is very easy and powerfull. If you want to use querystrings, its not done with the ols Request.Querystrings, but rather like this:

publicIEnumerable<GALV_ACCOUNT> GetAllAccounts()

{

    var q = Request.GetQueryNameValuePairs().Where(nv => nv.Key =="q").Select(nv => nv.Value).FirstOrDefault();

    if (q != null && q != string.Empty)

    {

        var result = accounts.Where(a=>a.Name.ToLower().StartsWith(q.ToLower()));

        return result;

    }

    else

    {

        thrownewException("Please specify a search query");

    }

}

Then you can call the method like this:

http://myserver/Accounts?q=p

 


UIButton with gradient background and image on it!

I got a request for an app with lots of gradient styles on all kinds of controls.

When I applied the typical code to the UIButton, the text stayed on but the image disappeared.

So I found that the solution is to bring the UIImageview to the front like this:

[vw bringSubviewToFront:vw.imageView];

Now the complete code to make the UIButton look like this:

is this:

+ (void) MakeNormalButton:(UIButton *) vw

{

    CAGradientLayer *gradient = [CAGradientLayerlayer];

    gradient.frame = vw.bounds;

    gradient.colors = [NSArrayarrayWithObjects:(id)[[UIColorwhiteColor] CGColor], (id)[[UIColorlightGrayColor] CGColor], nil];

    [vw.layer insertSublayer:gradient atIndex:0];

    vw.layer.masksToBounds = NO;

    vw.layer.shouldRasterize = YES;

    [vw.layersetShadowColor:[UIColorblackColor].CGColor];

    [vw.layersetShadowOpacity:0.5];

    [vw.layersetShadowRadius:2.0];

    [vw.layersetShadowOffset:CGSizeMake(2.0f, 2.0f)];

    [vw.layersetCornerRadius:5.0f];

    [vw bringSubviewToFront:vw.imageView];

 

}