How to create textblock in controller with XAMLReader - xamlreader

I want to create a textblock in controller with XAML code. Is it possible to do with this? Any other idea?
// Create a string
String sb = "<TextBlock Name='NameLabel' HorizontalAlignment='Left' TextWrapping='Wrap' FontSize='37.333' FontFamily='Intel Clear Light' Opacity='0.5' Width='277' Grid.Row='4' Grid.ColumnSpan='3' Grid.Column='1' Text='Name (as per IC)' Margin='1,0,0,80' Grid.RowSpan='2'/>";
// Create a textblock using a XamlReader
TextBlock myTextBlock = (TextBlock)XamlReader.Load(sb.ToString());
// Add created button to previously created container.
splMain.Children.Add(myTextBlock);

I have change my code. And now I have an error of [ splMain.Children.Add(myTextBlock); Additional information: Object reference not set to an instance of an object.]
// Create a string
String sb = "<TextBlock xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' Text=\"Name (as per IC)\" Margin=\"1,0,0,80\" Grid.RowSpan=\"2\"/>";
// Create a textblock using a XamlReader
TextBlock myTextBlock = (TextBlock)XamlReader.Load(sb.ToString());
// Add created button to previously created container.
splMain.Children.Add(myTextBlock);

Related

EF6, DbContext/POCO setting virtual nav property to attach new object: possible?

I am in the process of switching a very old EF6 edmx/ObjectContext implementation over to DbContext/POCO. I was able to generate the POCO model and all read operations seem to be working now.
My problem is that I have thousands of lines of code that do things like this:
var person = db.Persons.Single(x=> x.Id = 45);
var phone = new Phone { Person = person, Number = "123-456-7890"};
In the old edmx/ObjectContext world, when I set Person = person, the State of phone would be Added.
In the new DbContext/POCO world, this set has no effect at all, and the state remains Detached.
The new model code is using something like this to create the navigation property:
public virtual Person Person { get; set; }
Whereas the old edmx code was a getter setter, and the setter was doing this:
((IEntityWithRelationships)this).RelationshipManager
.GetRelatedReference<Phone>("Model.fk_Person_Phone", "Person")
.Value = value;
Is there any way to make the virtual nav property work similarly to the old method, and attach the new object to the database?

Swift Generics - Changing Values and Passing Between View Controllers

I'd like to use a variable of type Any in order to pass different classes to a child view controller. For example, I might have Table, Chair and Plate objects. In my child view controller, I'd like to change the value of one of their properties (e.g. Table.legs was 4, change that to 3), and for the parent view controller to be able to read that from the child VC. I'll use a Protocol to update the parent VC that can pop the child after reading the updated object.
In order to work out how the passing of generics might work, I wrote this code in a playground:
class Table {
var legs: Int
var material: String
init(legs: Int, material: String) {
self.legs = legs
self.material = material
}
}
var anObject: Any?
// set up the Table
let aTable = Table(legs: 4, material: "Oak")
// set anObject to be the Table
anObject = aTable
// get the object and change it
let bTable = anObject as! Table
bTable.legs = 3
// get the original object and cast it as a Table
let cTable = anObject as! Table
print(cTable.legs) // prints 3
I believe from this, I should be able to do what I describe above without any issues, because the original object reference (anObject) is updated whenever I update a variable referencing it.
My question is this - are there any pitfalls I should be aware of when adopting this approach? It appears that rather than creating a copy of an object, swift will always create a pointer to the original object; are there any situations when that does not hold true?
Appologies if this is seen as a fairly basic question, but this is all fairly new to me - many thanks in advance!
Class are reference types as you noticed, if you assign an instance of the class to a variable, it keep the reference (the pointer in memory) to the instance and not the value copy.
Struct are value types, if you copy the instance of the structure to another variable, it's just copied to the variable.

It seems like each View Controller is creating a unique instance of my struct - which I don't want?

I'm creating an app in Swift 2.0 xCode7 using the Tabbed-Application template, with each screen having a separate ViewController. I have a struct to manage a variable I want to be accessed by all view controllers. I created the instance of the struct in the first view controller. I'm able to access the struct data and methods in the other views, but if update the data in one view, it doesn't change for all... It's acting as if each View Controller is creating its own instance on its own. I don't want that. I want each ViewController to share the same updated data in the struct. Does this mean that I should be creating a Singleton Pattern? Or, something else? I'm quite new at this, so thanks for your patience.
I'm not sure how exactly you access the structure but it might be that you only need to change struct to class because structs are value types so if you assign it or pass into a method it is copied whereas an instance of a class will avoid copying
Because you didn't give me any code, this is just my guess.
Structs are different from classes. The former stores values and the latter stores references. Let's look at this code:
var obj = SomethingCool()
obj.somethingCooler = 20
var obj2 = obj
obj2.somethingCooler = 10
If SomethingCool were a struct, obj.somethingCooler would still be 20 but obj2.somethingCooler would be 10. On the other hand, if SomethingCool were a class, both obj.somethingCooler and obj2.somethingCooler would be 20.
This is because the third line. The third line is VERY important. If SomethingCool were a struct, the values stored in obj will be copied to obj2. i.e. Two set of independent values would be created. If it were a class, the object that obj will also be referenced by obj2. i.e. There would still be just one object.
Now that you know the difference, I can tell you that you must have done something like the third line in your view controllers, haven't you?
To solve this problem, you can change from a struct to a class. Or you can create something like this:
public class SomeName {
static var myData: SomeTypeOfStruct {
return something
}
}
If you are so hellbent on keeping it as a struct you could do something that swift actually helps u out with.....AppDelegate!
The appdelegate.swift is a single instance object for any application. So in case you want to save a value that you need to access throughout the application or update throughtout the application, you might want to use AppDelegate.
E.g.
In FirstViewController.swift set the AppDelegate variable that you want to reflect on the remaining screens:
(UIApplication.sharedApplication().delegate as! AppDelegate).commonVariableName = NewValueYouWant;
In the SecondViewController.swift, take up that value from the AppDelegate
var updatedValue = (UIApplication.sharedApplication().delegate as! AppDelegate).commonVariableName;
Again...as #Sweeper said, you can always switch to class which is more reliable and used to achieve something like this.
It's acting as if each View Controller is creating its own instance on
its own.
It's all explained in Apple's Swift guide:
Structs:
struct Dog {
var name: String
}
var d1 = Dog(name: "Rover")
var d2 = d1
d2.name = "Sally"
print(d1.name)
print(d2.name)
--output:--
Rover
Sally
Classes:
class Cat {
var name: String = ""
}
var c1 = Cat()
c1.name = "Kitty"
var c2 = c1
c2.name = "Gerald"
print(c1.name)
print(c2.name)
--output:--
Gerald
Gerald
See the difference?

Sharing data between VIewControllers - iOS

For any object created I generally use two two scopes 1) Singleton 2) {local scope}. I am looking for something in between.
Say I have one object that 5 view controllers are editing. I want to share an object between view controllers without having to pass it between view controllers. But it should not also live throughout application since once I am done editing the object i don't need it anymore.
I don't want to inherit all view controller from another class an create a variable there. Since view controller are reusable for different objects. I want to create an object that comes to life before launch of first view controller, lives throughout the scope of 5 view controllers and then dies after I have saved it someway. Is there anyways I could do this in iOS.
An alternative is to use your AppDelegate. Within it you can declare a global var than 2 functions, a first one to get the current value and another one to set the value.
It might give something like this:
// Get AppDelegate instance
let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate;
// Use your getter to get the current value
var something = appDelegate.getYourStuff();
// Or use a setter to set it, a modifier to modify oit
appDelegate.setYourStuff(yourStuff);
appDelegate.modifiyYourStuffAttribute(newAttributeValue);
Don't realize if such a method is a bad practice or not, but it works for me.
Open to other suggestions!
As Mat said you can do is in that what. For me better is to create specific class for that that will do one particular job.
class EditingSession {
class Factory {
private static let session = EditingSession() //do it lazy
static func create() -> EditingSession {
return session
}
}
func openSession() {
}
func endSession {
}
func getData () -> AnyObject {
...
}
}
In editing session create private initializer. Factory should give the shared instance.
You can pass this class to your ViewControllers and manipulate with the data. You can inject it or just set as property in your VC or VM.
If you are done with editing you should end session and clear data or other stuff.

Value from 1st class not in 2nd class

I have two classes, Step1 and Step2. Step1 contains a textfield called wrp. When the user enters a number in wrp in Step1, I would like to be able to work with it in Step2. Here is what I have tried (this is code in Step2):
int AdditionalDays;
Step1ViewController *wrp1 = [Step1ViewController new];
UITextField *wrp = [wrp1 wrp];
AdditionalDays = [wrp.text intValue];
TotalTotal.text = [[NSString alloc] initWithFormat:#"%i", AdditionalDays];
The app does not throw an error message, but the user-entered number in wrp in Step1 is disregarded in Step2. I don't know why it is not working. Any suggestions?
Because you create a new Step1ViewController
In the line Step1ViewController *wrp1 = [Step1ViewController new]; you are creating a new instance of Step1ViewController. What you need to to is get a reference to the original Step1ViewController the user entered the text in.
To do this, you would probably create a property in your Step2ViewController where you can pass in either the Step1ViewController, or even better, the value the user entered.
So in your Step2ViewController class header, add a property like this:
#property (assign) int additionalDays;
Then, when you create the Step2ViewController to display it on screen, you set this property to the value the user entered, like this:
Step2ViewController *controller = [Step2ViewController new];
controller.additionalDays = [wrp.text intValue];
// push the controller on a UINavigationController or something
The reason that this is happening is that in Step2 you have said:
Step1ViewController *wrp1 = [Step1ViewController new];
UITextField *wrp = [wrp1 wrp];
Unfortunately this isn't doing what you want it to do. Your app doesn't know what you're trying to do and so what the code is actually doing is "Please create a brand new Step1 and also create a brand new TextField, then give me the value of this brand new empty TextField".
Instead of being inside Step2 and working backwards to get the TextField, try to be inside Step1 and pass the value to Step2.

Resources